Professional Documents
Culture Documents
Teacher-Student Activities
In this class, we are going to improve our Mind Reader algorithm so that it can make better
predictions instead of making predictions randomly.
In this new algorithm, we will store the count of the player inputs. Essentially, we will keep a count
of 0 and 1 . At every player input, we will check which count value is greater between the counts of
0 and 1 .
If the count of 0 is greater, the computer will predict 0 . Else if the count of 1 is greater, the
computer will predict 1 .
Else if the count values are same, the computer will predict 0 or 1 randomly.
Before we look at the steps for improving our algorithm, let's quickly revise variables and functions.
Can you recall what variables and functions are and how do they work?
count_zero - to store the count of the number of times a player has entered 0 .
count_one - to store the count of the number of times a player has entered 1 .
3. Create another function and name it update_counts() to update the count_zero and
count_one values based on the player inputs.
4. Create another function and name it update_scores() to update the player_scores and the
computer_scores as the game progresses.
If the predicted value is same as the player_input value, increase the computer_score
value.
5. Finally, create one more function and name it gameplay() to run the game.
By the end of this class, we will create an improved version of the algorithm which is shown in the
code below.
import random
count_zero = 0
count_one = 0
player_score = 0
computer_score = 0
def prediction():
if count_zero > count_one:
predict = 0
elif count_one > count_zero:
predict = 1
else:
predict = random.randint(0, 1)
return predict
def update_counts(player_input):
global count_zero, count_one
if player_input == 0:
count_zero = count_zero + 1
else:
count_one = count_one + 1
player_input = int(player_input)
update_counts(player_input)
update_scores(predicted, player_input)
if player_score == 10:
print("Congrats, You Won!")
break
elif computer_score == 10:
print("Bad Luck, You Lost!")
break
count_zero - to store the count of the number of times a player has entered 0 .
count_one - to store the count of the number of times a player has entered 1 .
Activity 2: Functions^^
Now we need to create four functions:
Unfortunately, we don't have a ready-made function (like the print() and input() functions) to do
this. Hence, we need to create these four functions by ourselves.
So before creating these functions, rst let's learn the Python functions in detail.
A function is like a machine which takes some input and gives some output. For example: An ATM
machine is a function which takes your debit card and your pin number as inputs and returns the
cash as an output.
Similarly, in Python, a function takes some input (not always needed) and returns some output
(depends whether you want the function to return an output).
To create a user-de ned function, we use a keyword called def followed by the name of the
function, followed by common brackets, followed by the colon (:) symbol.
The input(s) to the function is/are provided within the common brackets.
Note:
1. The function attributes must be written in the subsequent lines after leaving two blank spaces
between the code cell and the code. To exit the function, remove the blank spaces between
the code cell and the code in the subsequent lines.
To understand functions better, let's create a function which takes two numbers as inputs and
returns their addition as an output.
1 # Teacher Action: Write a function which adds two numbers and returns their addition as an
2 def add_numbers(num1, num2):
3 result = num1 + num2
4 return result
5
6 add_numbers(45,12)
57
1 res = add_numbers(12,7)
2 print(res+5)
24
1 print("res:",res)
2 print("result:",result)
3
res: 19
-------------------------------------------------------------------------
--
NameError Traceback (most recent call
last)
<ipython-input-14-a68d5596b5c7> in <module>()
1 print("res:",res)
----> 2 print("result:",result)
Here:
1. The add_numbers() is a function which takes two inputs, num1 and num2 . You can provide any
name of your choice to the function. However, some of the function names are reserved for
the ready-made functions in Python. So, you need to avoid those names for naming a function
such as print(), input(), sum() etc. For the sake of convenience, we have chosen
add_numbers as the name of the function.
2. The + operator adds the values stored in the num1 and num2 variables.
3. The variable result stores the sum of the values stored in the num1 and num2 variables.
4. The return keyword returns the output. If you don't write the return statement, then this
function will only add the numbers but won't give any output.
5. While creating a function, make sure that you follow indentation, i.e., after declaring the
function, leave two blank spaces between the code cell and the code in the subsequent lines.
Otherwise, your code will throw the IndentationError at the time of its execution.
6. To exit from the function, remove the blank spaces between the code cell and the code in the
subsequent lines.
For some problem statements, a function doesn't take an input, yet it produces at the output. E.g.,
you can create a function which prints your name whenever you call it.
Let's create a function which returns "WhiteHat Jr" whenever you call it.
1 # Student Action: Create a function and name it "no_input()". It should not take any input
2 def no_input():
3 return "Whitehat Jr."
4 print (no_input())
Whitehat Jr.
As you can see, we called the no_input() function thrice and thrice it returned an output without
taking an input.
Now we are in the position to create the four functions. Let's start with the prediction() function.
1 # Student Action: Create the 'prediction()' function. It should not take any input and sho
2 import random
3 def prediction():
4 if count_zero > count_one:
5 return 0
6 elif count_one > count_zero:
7 return 1
8 else:
9 return random.randint(0,1)
10
11 prediction()
Currently, since both the count_zero and count_one values are 0 (or equal), the prediction()
function will return 0 or 1 randomly. Run the above code again and again to observe the function
behaviour.
The update_counts() function should not return anything. It should just update the values stored in
the count_zero and count_one variables.
Note: To modify the value stored in a variable using a user-de ned function, we need to declare that
variable with the global keyword inside the function. The variables declared outside user-de ned
functions are called the global variables. However, the variables declared inside a user-de ned
function is called a local variable. The creators of Python have restricted user-de ned functions to
modify the values stored in the global variables directly without declaring them as global in a
function.
In this case, we are going to modify the count_zero and count_one values using the
update_counts() function. Hence, we need to add the global count_zero, count_one statement
inside the function.
1 # Student Action: Create the 'update_counts' function. It should take 'player_input' varia
2 def update_counts(player_input):
3 global count_zero, count_one
4 if player_input == 0:
5 count_zero = count_zero + 1
6 else:
7 count_one = count_one + 1
Now let's test the above function by giving 0, 0 and 1 values as the inputs to the
update_counts() function and printing the values stored in the count_zero and count_one
variables.
2
1
If the predicted value is same as the player_input value, increase the computer_score by 1 .
The predicted value is obtained from the prediction() function. Immediately after updating the
scores, the update_scores() function must print the updated values stored in the player_score
and computer_score variables.
Note: To modify the value stored in a variable using a user-de ned function, we need to declare that
variable with the global keyword inside the function.
In this case, we are going to modify the player_score and computer_score values using the
update_scores() function. Hence, we need to add the global player_score, computer_score
statement inside the function.
1 # Student Action: Create the 'update_scores()' function. It should take the 'predicted' an
2 # It should update the 'player_score' and 'computer_score' values and should print them. I
3 def update_score(prediction, player_input):
4 global player_score, computer_score
5 if prediction == player_input:
6 computer_score = computer_score + 1
7 else:
8 player_score = player_score + 1
9 print("player score is", player_score)
10 print("computer score is", computer_score)
Let's test the above function by giving 0 as an input to the update_counts() and update_scores()
functions and then printing the values stored in the player_score and computer_scores variables.
Initially, count_zero value is the same as the count_one value. So, the prediction() function will
predict 0 or 1 randomly. The computer_score value will be equal to 1 if the predicted value is
same as the player_input , i.e. 0 . Else the player_score will be equal to 1 .
The second time, if you again give 0 as another input, the prediction() function will return 0 as
the predicted value because the count_zero = 2 which is greater than count_one value.
So, the computer_score value should be atleast 1 , i.e. either 1 or 2 after the second input.
1 # Student Action: Get the predicted value from the 'prediction()' function and store it in
2 # Then enter 0 as an input to the 'update_counts()' and 'update_scores()' functions twice.
3 count_zero = 0
4 count_one = 0
5
6 player_score = 0
7 computer_score = 0
8
9 # First input.
10 predict = prediction()
11 print(predict)
12 update_counts(0)
13 update_score(predict, 0)
14 print() # This will add an emtpy line.
15
16 # Second input.
17 predict = prediction()
18 print(predict)
19 update_counts(1)
20 update_score(predict, 1)
1
player score is 1
computer score is 0
0
player score is 2
computer score is 0
As you can see, after the second input, the computer_score value is at-least 1 . Run this code again
and again to observe the behaviour.
In this function, you have to code a few of the functional requirements. They are as follows:
1. Get the predicted value using the prediction() function and store it in the predicted
variable.
2. Take input from a player using the input() function and store it in the player_input variable.
Print "Enter either 0 or 1: " while taking the input.
4. Update the values stored in the count_zero and count_one variables using the
update_counts() function.
5. Update the values stored in the player_score and computer_score variables using the
update_scores() function.
1 # Student Action: Run the game by calling the 'gameplay()' function at the end of the code
2 import random
3
4 count_zero = 0
5 count one = 0
5 count_one 0
6
7 player_score = 0
8 computer_score = 0
9
10 def prediction():
11 if count_zero > count_one:
12 predict = 0
13 elif count_one > count_zero:
14 predict = 1
15 else:
16 predict = random.randint(0, 1)
17
18 return predict
19
20 def update_counts(player_input):
21 global count_zero, count_one
22 if player_input == 0:
23 count_zero = count_zero + 1
24 else:
25 count_one = count_one + 1
26
27 def update_scores(predicted, player_input):
28 global player_score, computer_score
29 if predicted == player_input:
30 computer_score = computer_score + 1
31 print("Computer Score:", computer_score)
32 print("Player Score:", player_score)
33 else:
34 player_score = player_score + 1
35 print("Computer Score:", computer_score)
36 print("Player Score:", player_score)
37
38 def gameplay():
39 valid_entries = ['0', '1']
40 while True:
41 # Get the predicted value and stored it in the 'predicted' variable.
42 predicted = prediction()
43 # Take input from the player and store it in the 'player_input' variable.
44 player_input = input("Enter either 0 or 1: ")
45 while player_input not in valid_entries:
46 print("Invalid Input!")
47 player_input = input("Please enter either 0 or 1: ")
48
49 # Convert the 'player_input' value to an integer value.
50 player_input = int(player_input)
51
52 # Update the 'count_zero' and 'count_one' values using the 'update_counts()' function.
53 update_counts(player_input)
54
55 # Update the 'player_score' and 'computer_score' values using the 'update_scores()' fu
56 update_scores(predicted, player_input)
57
58 if player_score == 10:
59 print("Congrats, You Won!")
60 break
61 elif computer_score == 10:
62 print("Bad Luck, You Lost!")
63 break
64
65 # Call the 'gameplay()' function to run the game.
66 gameplay()
Enter either 0 or 1: 0
Computer Score: 1
Player Score: 0
Enter either 0 or 1: 1
Computer Score: 1
Player Score: 1
Enter either 0 or 1: 0
Computer Score: 1
Player Score: 2
Enter either 0 or 1: 0
Computer Score: 2
Player Score: 2
Enter either 0 or 1: 0
Computer Score: 3
Player Score: 2
Enter either 0 or 1: 0
Computer Score: 4
Player Score: 2