Number Guessing Game

Number Guessing Game


  Machine Learning in Python

Summary A simple game written in python where we will learn

  • Functions
  • loops
  • break statement
  • if statement
  • boolean values
  • type conversion
  • program blocks (indentation)

Contents

Here is the program

# In this program we will learn about
# 1. Functions
# 2. while Loop
# 3. break statement
# 4. if statement
# 5. boolean values
# 6. type conversion

secret_num = 59
print ( " Guess a number between 1 and 100")
num = input ()

while ( True ):
    
    if   int(num) > sceret_num :
        print ( " guess a lower number " )
        num = input ()

    if int(num) < secret_num : 
        print ( " guess a higher number" )
        num = input ()
        
    if int(num) == secret_num : 
        print ( " Hurray !! you guessed it ")
        break

Game Logic

This is a computer version of the game we used to play as kids – I hold a secret number between 1 and 100 and you have to guess what it is. Every time you make a guess, I give you a clue – if your guess is greater than the secret I tell you that it is greater and if it is less I tell you it is less. How fast you get to the secret number is how you win the game.  

Code Explanation

  • Ideally, we should let the computer pick a random number between 1 and 100, but for now, let’s hard code it to some random number.
secret_num = 59 

we are essentially storing the number 59 in a place called secret_num. In programming language these places are called variables.

  • Next, we need to tell the user what to do – basically print an instruction on the screen. For that you use a built-in function in python called print
print ( "Guess a number between 1 and 100" )

function is like a black box – You ask it do something and it does it for you. You don’t care how you do it. For example, in this case, we are asking python to print a piece of text on the screen. How it prints it is something we don’t care about. We just need to know how to use it.

  • Next, we store the user’s guess into a variable.
num = input ()

Here we are using a built-in python function called input() to get an input from the user playing the game and storing it in the variable num

  • Next, we do the comparison – If the guess is greater than the secret or less.
if  int(num) > secret_num :   
    print ( " guess a lower number " )  
    num = input () 

if statement evaluates an expression to be either True or False. In this case we are comparing if the guessed number is greater and if it is we are printing an instruction on the console to the user. And we are ready to take another input from the user.

The number could be less too. In which case, we need to print a different instruction again on the console and accept another number from the user.

if int(num) < secret_num :  
   print ( " guess a higher number" )  
   num = input ()

In case the user did the correct guess, the game is over.

if int(num) == secret_num :  
    print ( " Hurray !! you guessed it ") 
    break 

In case you are wondering about the == (double equal to ) sign, in programming this means that it is not an assignment, but a comparison.

  • And what about the break statement ? What are we breaking away from ? The while loop

We don’t know how many guesses the user would take to guess the right number. We have to keep asking the user to make guesses until he guesses the right number. That is where the while statement comes in. It keeps going on and on until you break out of it using the break statement.

That’s it – simple enough, right ? In just about 15 lines, we were able to write a simple game.

  • And what about indentation ? You see the program being neatly indented using tabs or spaces. If you look at the program in visual studio code, you will see that the editor has neatly drawn lines demarcating different logical sections or blocks of code.
Visual Studio Code

Visual Studio Code

Separating blocks of code is done in different ways in different languages. For example, in C language or Java language flower brackets { } are used.

if ( num == secret_num ) 
 {
   printf ( " guess a higher number" )  
   num = input () 
 } 

python uses indentation as a way of separating blocks of code. Just for fun, try to mess up the indentation and see what happens.

secret_num = 59  
   print ( " Guess a number between 1 and 100") 
num = input ()

For example, just indent the print statement one tab out. Immediately, you will see the python linter in visual code complaining that this is an unexpected indent.

Python Linter
Python Linter

In case you execute the program, it will show you the same error as well.

line 10 print ( " Guess a number between 1 and 100")    
 ^ IndentationError: unexpected indent

Here is a visual demonstration of how code blocks go in a hierarchical fashion.

Python Code Blocks
Python Code Blocks

Blocks 2,3 and 4 are all inside block 1.

%d bloggers like this: