You are on page 1of 13

Lesson 3: Mind Reader Game - Improving The Algorithm

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?

To create the improved algorithm we will follow 5 steps. They are:

1. Create four variables:

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 .

player_score - to keep the score of a player.

computer_score - to keep the score of the computer.

2. Create a function and name it prediction() . This is to predict 0 or 1 as an output by


comparing the values stored in the count_zero and count_one variables.

If count_zero > count_one , the function should return 0 .

Else if count_one > count_zero , the function should return 1 .

Else if count_zero == count_one , the function should return 0 or 1 randomly.

3. Create another function and name it update_counts() to update the count_zero and
count_one values based on the player inputs.

If the player input is 0 , the count_zero value should increase by 1 .

Else if the player input is 1 , the count_one value should increase by 1 .

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.

Else, increase the player_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

def update_scores(predicted, player_input):


global player_score, computer_score
if predicted == player_input:
computer_score = computer_score + 1
print("Computer Score:", computer_score)
print("Player Score:", player_score)
else:
player_score = player_score + 1
print("Computer Score:", computer_score)
print("Player Score:", player_score)
def gameplay():
valid_entries = ['0', '1']
while True:
predicted = prediction()
player_input = input("Enter either 0 or 1: ")
while player_input not in valid_entries:
print("Invalid Input!")
player_input = input("Please enter either 0 or 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

Activity 1: Initialise The Variables


Let's create four variables:

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 .

player_score - to keep the score of a player.

computer_score - to keep the score of the computer.

1 # Student Action: Create the 'count_zero', 'count_one', 'player_score' and 'computer_score


2 count_zero = 0
3 count_one = 0
4 player_score = 0
5 computer_score = 0

Activity 2: Functions^^
Now we need to create four functions:

The prediction() function to predict 0 or 1 .

The update_counts() function to update the count_zero and count_one values.

The update_scores() function to update the player_score and computer_score values.

The gameplay() function to run the game.

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.

The syntax for creating a user-de ned function is

def function_name(input1, input2, ..., inputN):

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.

2. The function name must begin with a lowercase letter.

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)

NameError: name 'result' is not defined

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.

Activity 3: The prediction() Function


The prediction() function should predict 0 or 1 as output by comparing the values stored in the
count_zero and count_one variables.

If count_zero > count_one , the function should return 0 .

Else if count_one > count_zero , the function should return 1 .

Else if count_zero == count_one , the function should return 0 or 1 randomly.

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.

Now, let's create the update_count() function.


Activity 4: The update_counts() Function And The global Keyword
The update_counts() function should update the values stored in the count_zero and count_one
variables based on the player inputs.

If the player_input == 0 , the count_zero value should increase by 1 .

Else if the player_input == 1 , the count_one value should increase by 1 .

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.

The output should be count_zero = 2 and count_one = 1

1 # Student Action: Enter 0, 0 and 1 as inputs to the 'update_counts()' function one-by-one.


2 # Print the values stored in the 'count_zero' and 'count_one' variables.
3 update_counts(0)
4 update_counts(0)
5 update_counts(1)
6 print(count_zero)
7 print(count_one)

2
1

Similarly, let's create the update_scores() function.

Activity 5: The update_scores() Function^


The update_scores() function should take the predicted and player_input values as inputs and
should update the player_scores and the computer_scores by comparing the predicted value
with the player_input value.

If the predicted value is same as the player_input value, increase the computer_score by 1 .

Else, increase the player_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.

Now, let's create the gameplay() function.

Activity 6: The gameplay() Function^^^


The gameplay() function will run the game. Half of this function will be provided to you. The
remaining half you will have to code.

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.

3. Convert the player_input value to an integer value.

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: Create the 'gameplay()' function.


2 def gameplay():
3 valid_entries = ['0', '1']
4 while True:
5 # Get the predicted value and store it in the 'predicted' variable.
6 predicted = prediction()
7 # Take input from the player and store it in the 'player_input' variable.
8 player_input = input("Enter either 0 or 1")
9 while player_input not in valid_entries:
10 print("Invalid Input!")
11 player_input = input("Please enter either 0 or 1: ")
12
13 # Convert the 'player_input' value to an integer value.
14 player_input = int(player_input)
15 # Update the 'count_zero' and 'count_one' values using the 'update_counts()' function.
16 update_counts(player_input)
17 # Update the 'player_score' and 'computer_score' values using the 'update_scores()' fu
18 update_score(predicted, player_input)

Now, test the game by putting everything together in one place.

Activity 7: Running The Game


Let's run the game using the gameplay() function. We will run this game only until either the
computer or the player has reached a score of 10 . Otherwise, the testing will take a very long time.

You can replace 10 with 50 after testing the game.

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

You might also like