0% found this document useful (0 votes)
63 views11 pages

Lesson 1: Mind Reader Game - Variables & Data-Types: Teacher-Student Activities

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Lesson 1: Mind Reader Game - Variables & Data-Types

Teacher-Student Activities
Before creating the actual algorithm for the Mind Reader game, let's try to build a very simple
algorithm in which a computer randomly predicts a player's next move. In the subsequent classes,
we will learn the actual algorithm.

In the very simple version of the Mind Reader game algorithm, a computer randomly predicts Heads
or Tails for every player input.

An algorithm is a well-de ned and well-structured logic written in very clear and concise steps. For
e.g., in case of the Mind Reader game, we want the computer to randomly predict the player inputs.
Therefore, the very simple version of the Mind Reader game algorithm should implement the
following steps:

1. The computer should ask a player to enter either 0 or 1 as an input where 0 stands for
Heads and 1 stands for Tails .

2. The computer should print the player input.

3. Regardless of the player input, the computer should predict 0 or 1 randomly where 0 stands
for Heads and 1 stands for Tails .

4. The computer should print the value predicted by the computer.

After following the above three steps, we should get the following algorithm:

import random

player_input = input("Enter either 0 or 1: ")


print("You entered", player_input)
predict = random.randint(0, 1)
print("Computer predicted", predict)

Note: An algorithm is not speci c to a computer programming language. It is just a logic that can
be implemented in any computer programming language.

Activity 1: The print() Function


As part of the algorithm, we need to print two messages to the player of the Mind Reader game.
They are:

1. "You entered"

2. "Computer predicted"

Let's learn how to do that. In Python, you can print anything using the print() function.

What is a function?

A function is like a device which takes some input and produces some output. For e.g., a juicer is a
device which takes a fruit as an input and produces the juice of that fruit as an output. We will learn
about functions in detail in the subsequent classes.

Let's print "You entered" message using the print() function. The text "You entered" is an input
to the print() function. The output would also be the same.

When using the print() function, enclose the text that you need to print either in the single-quotes
( '' ) or in double-quotes ( "" ).

To run the code, either click on the Play button on the left-hand side of the code cell or press the
shift and enter keys together while you are active in the code cell.

1 # Teacher Action: Print "You entered" message to a player using the 'print()' function.
2 print("You entered")

You entered

So, we have printed "You entered" . Notice that the text written after the hash ( # ) symbol is not a
code rather it is a plain text. Anything written after # is read as a plain text by Python. Such plain
texts are called comments made by a coder to brie y explain the code.

Now you print "Computer predicted" message using the print() function.

1 # Student Action: Print "Computer predicted" message to a player using the 'print()' funct
2 print("Computer predicted")

Computer predicted

Activity 2: Variables And Data-Types^


As part of the algorithm, the Mind Reader game should take input (either 0 or 1 where 0 indicates
Heads and 1 indicates Tails ) from a player and store it somewhere so that we can use the input
value later. To store a value, we need to create a variable.

What is a variable?

A variable is like a container.

For example, a mug stores coffee or tea, a ti n box stores your school lunch.
Similarly in Python, a variable stores a value which is either a text (for e.g., "You entered" ) or a
number or any other data-type.

What are data-types in Python?

The data types in Python are as follows:

1. An integer (denoted as int ): a number without the decimal point. For e.g., 0, 1, -234,
12500 etc.

2. A oating-point number (denoted as float ): a number having a digit after the decimal point.
For e.g., 0.0, 1.0, 3.14, 1.618 etc.

3. A string (denoted as str ): a value enclosed between either single-quotes ( '' ) or double-
quotes ( "" ). For e.g., "Computer predicted" , 'Windows 10' , 'Xbox 360' etc.

4. A boolean (denoted as bool ): either True or False value. The 'Yes' or 'No' values are
generally represented as True or False in Python. It cannot understand Yes or No because
that's how Python has been designed by its creators.

Note:

1. The T in True and F in False must be capital because Python is case-sensitive.

2. A variable can store only one value at a time.

Now, let's practice Python variables by storing the various information about Mercury and Saturn
planets in variables.

1 # Teacher Action: Create 4 different variables and store the planet name, diameter, gravit
2 # 1. Store a string value in a variable
3 planet1_name = "Mercury"
4 # 2. Store an integer value in a variable.
5 planet1_dia = 4879
6 # 3. Store a float-point number in a variable.
7 planet1_gravity = 3.7
8 # 4. Store a boolean value in a number.
8 # 4. Store a boolean value in a number.
9 # Python can also store a boolean value, i.e., either True or False.
10 # The 'Yes' or 'No' values are generally represented as True or False in Python. It cannot
11 planet1_has_ring = False
12 # If we run the code cell, we won't get any output. But the values will be assigned to the

So far we have stored 4 different types of values in 4 different variables. Now, let's print the values
stored in these variables using the print() function.

1 # Teacher Action: Print the values stored in all the 4 variables.


2 print(planet1_name)
3 print(planet1_dia)
4 print(planet1_gravity)
5 print(planet1_has_ring)

-------------------------------------------------------------------------
--
NameError Traceback (most recent call
last)
<ipython-input-3-60a3ec71c339> in <module>()
1 # Teacher Action: Print the values stored in all the 4 variables.
----> 2 print(planet1_name)
3 print(planet1_dia)
4 print(planet1_gravity)
5 print(planet1_has_ring)

NameError: name 'planet1_name' is not defined

As a good practice, name a variable in such a way that it re ects the nature of the value stored in it.
In this case, the value that needs to be stored is the diameter of a planet. So, we have named a
variable planet1_dia suggesting that the value stored in it is indeed the diameter of a planet.

This is just a good practice. It is NOT compulsory to name a variable in this manner. You are free to
provide any name to your variable.

Note:

The rst letter of a variable must be a lowercase letter. Otherwise, your variable name may
clash with the existing prede ned Python functions and objects.

Never name a variable beginning with a number. For e.g., the variable 1st_prize_winner
begins with the number 1 . Avoid creating variables with such names as it is a very bad
practice.

As part of the naming process of a variable, never provide spaces between two or more letters
or words because Python will consider them as different variables. For e.g., the variable
planet1 dia are actually two different variables. So either provide no space between two
words or letters in a variable or add the underscore ( _ ) sign between them.
Now, it's your turn to write the codes.

1 # Student Action: Create 4 different variables and store the planet name, diameter, gravit
2 # 1. Create a variable and name it 'planet2_name'. Store the value "Saturn" in it.
3 planet2_name = 'Saturn'
4 # 2. Create a variable and name it 'planet2_dia'. Store the diameter value of Saturn in it
5 planet2_diameter = 120536
6 # 3. Create a variable and name it 'planet2_gravity. Store the gravity value of Saturn in
7 planet2_gravity = 9.0
8 # 4. Create a variable and name it 'planet2_has_ring'. Store True in it.
9 planet2_has_ring = True
10 # Print the values stored in all the 4 variables.
11 print(planet2_name)
12 print(planet2_diameter)
13 print(planet2_gravity)
14 print(planet2_has_ring)

Saturn
120536
9.0
True

So in this way, we can store some information (or data) in a variable and print their values using the
print() function.

After the class, you can refer to the link provided in the Activities section under the title NASA
Planetary Fact Sheet to get the most accurate facts about the planets in our solar system.

Activity 3: The input() Function^^


As part of the algorithm, the Mind Reader game needs to ask a player to enter either 0 or 1 where
0 denotes Heads and 1 denotes Tails .

To take input from a user in Python, you can use another function called input() .
1 # Teacher Action: Take input from a user and store it in the 'player_input' variable.
2 player_input = input("Enter either 0 or 1")
3 print("You entered",player_input)
Let's ask the player of the Mind Reader game to enter either 0 or 1 and store it in the
Enter either 0 or 10
player_input variable.
You entered 0 Inside the input() function, you can also write a message to the player to
display the message before taking the required input.
Here,
In the using the let's
next line, also function,
print() we printed
print the value storeda in
message "You entered"
the player_input along with the value stored
variable.
in the player_input variable. The message and the player_input are separated by a comma.

You can print multiple entities using the print() function by separating them with a comma.

Now, you ask a player to enter their name using the input() function, display the "Enter your
name: " message while taking the input and store the name entered by the player in the
player_name variable. Then print the following message using the print() function.

"Hello, player_name ! Welcome to the Mind Reader Game."

where player_name is the name entered by the player.

1 # Student Action: Write a code to take input from a player and then print "Hello, player_n
2 # Create a variable and name it 'player_name'. Take input from the player using the 'input
3 player_name = input('Enter your name')
4 # Print "Hello, player_name ! Welcome to the Mind Reader Game."
5 print('Hello!', player_name, 'Welcome to the mind reader game.')

Enter your nameAditi Gautam


Hello! Aditi Gautam Welcome to the mind reader game.

Activity 4: Importing A Module


So far we have completed the rst two steps of creating the Mind Reader game algorithm, i.e.,
taking an input from a player and then printing the input given by the player.

import random
player_input = input("Enter either 0 or 1: ")
print("You entered", player_input)

For the next step, we need the computer to predict 0 or 1 randomly where 0 denotes Heads and 1
denotes Tails . For this step, we need a function called randint() which returns a random integer
between two integers. In our case, it must return either 0 or 1 randomly.

However, we can't use this function directly like the print() and input() functions. We rst need
to import a module called random .

Some functions in Python cannot be used directly. To use such a function, you rst need to import a
module in which that function exists and then use the required function.

For all practical purposes, a module is a collection of some special functions which are used
occasionally.

The syntax for using a special function which exists in a module is

import module_name

module_name.function_name

Note:

1. A syntax is like a grammar of a programing language. It is a standard protocol that must be


following while writing code.

2. Always import a module at the very beginning in your code.

3. A module needs to be imported only once. However, both the student and the teacher has to
import a module at-least-once at their respective ends.

4. The dot ( . ) operator is used to call a function from a module.

5. Based on your requirements you can use the relevant modules which are listed on the o cial
Python documentation. The link to the Python o cial documentation is provided in the
Activities section under the title Python O cial Documentation.

Let's try to generate a random number between 1 and 6 using the randint() function. It will return
either 1, 2, 3, 4, 5 or 6 . In case you have noticed, this is a simulation of a die roll.
1 # Teacher Action: Import the 'random' module to use the 'randint()' function to generate a
2 import random
3 die_roll = random.randint(1,6)
4 print(die_roll)

In the above code,

We have rst imported the random module

Then we have generated an integer between 1 and 6 and stored it in a variable called
die_roll .

Then, we have printed the value stored in the die_roll variable using the print() function.

You may get a different number as an output every time you run the above code.

Now, using the randint() function, you need to

generate 0 or 1 randomly and store it in a variable called predict .

Then, print the value stored in the predict variable using the print() function.

1 # Student Action: Using the 'randint()' function, generate 0 or 1 randomly.


2 # 1. Import the 'random' module.
3 import random
4 # 2. Generate 0 or 1 randomly and then store the value obtained in the 'predict' variable.
5 a = random.randint(0,1)
6 # 3. Print the value stored in the 'predict' variable.
7 print(a)

We have to import a module just once before using its functions. We don't have to import it again
and again.

Activity 5: The Very Simple Algorithm^^^


Now, we have successfully completed all the steps of creating a very simple Mind Reader game
algorithm. Let's put all the steps together in one place to run the algorithm.

We have to do the following:


1. Take input from the player using the input() function and store it in the player_input
variable. With the input() function, print the "Enter either 0 or 1: " message.

2. Using the print() function, print the "You entered" message along with the value stored in
the player_input variable.

3. Create a variable and name it predict . Store the value returned by the randint(0, 1)
function in the predict variable.

4. Using the print() function, print the "Computer predicted" message along with the value
stored in the predict variable.

And voila! We have our algorithm ready.


1 # Student Action: Put all the steps of creating the Mind Reader game algorithm in one plac
2 # 1. Create 'player_input' variable and store the input given by the player in it.
3 player_input = input('Enter either 0 or 1')
4 # 2. Print the "You entered" message along with the value stored in the 'player_input' var
5 print('You entered', player_input)
6 # 3. Create a variable called 'predict' and store the value returned by the 'randint(0, 1)
7 predict = random.randint(0,1)
8 # 4. Print the "Computer predicted" message along with the value stored in the 'predict' v
9 print('Computer predited', predict)

Enter either 0 or 10
You entered 0
Computer predited 0

So we have created a very simple algorithm in which a player enters either 0 or 1 and the computer
predicts 0 and 1 randomly.

In the subsequent classes, we will add the following functionalities to the our algorithm:

Keep a track of the scores for both the player and the computer.

Restrict a player from entering invalid inputs, i.e., inputs other than 0 or 1.

Continue running the game until one of the players reaches a score of 50

If the player wins, then print `"Congrats, You Won!` else print `"Bad Luck, You Lost!"` messages.

Additionally, we will improve the algorithm so that the computer can predict player moves
accurately.

Fun Fact
One of the cool things about Jupyter notebook is that you don't always have to use the print()
function to get an output. Let's say you want to print the value stored in a variable without using the
print() function, then you simply have to write the variable at the end of the code in a code cell.

1 # Student Action: Create a variable called 'game_name' and store the value 'Mind Reader Ga
2 # Print the value stored in the 'game_name' variable without using the 'print()' function.
3 game_name = 'Mind reader game'
4 game_name
5 predict

This functionality is not available in any other software for writing Python codes.

You might also like