Home » Python Programming » Python Program – Guess the number game

Python Program – Guess the number game

As we begin to wind down, I thought this week, we would have a Python program, and then review each part. This program is a guessing game. The player will attempt to guess the number (which is random and will always change), while being provided if their guess is high or low.

Let’s see the full program first, then break it down. Don’t be overwhelmed, we have actually covered most of this before.

# This is a guess the number game.
import random

guessesTaken = 0

print(‘Hello! What is your name?’)
myName = input()

number = random.randint(1, 20)
print(‘Well, ‘ + myName + ‘, I am thinking of a number between 1 and 20.’)

while guessesTaken < 6:
print(‘Take a guess.’) # There are four spaces in front of print.
guess = input()
guess = int(guess)

guessesTaken = guessesTaken + 1

if guess < number:
print(‘Your guess is too low.’) # There are eight spaces in front of print.

if guess > number:
print(‘Your guess is too high.’)

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print(‘Good job, ‘ + myName + ‘! You guessed my number in ‘ + guessesTaken + ‘ guesses!’)

if guess != number:
number = str(number)
print(‘Nope. The number I was thinking of was ‘ + number)

The first block of code calls for import random and assigns the variable guessTaken the value of zero. The import random will pull in the random function to be used a bit later.
import random
guessesTaken = 0
The second block of code is a simple print statement, and another variable, “myName” with the input. Recall this will take whatever the user values as their name and assign it to the variable myName.

print(‘Hello! What is your name?’)
myName = input()

This third black of code uses the function random, and creates a random number from 1 to 20. Then it assigns that value to the variable number. The second line prints out the string, “Well.. I am thinking of a number between 1 and 20”. The myName variable is used within the print statement.

number = random.randint(1, 20)
print(‘Well, ‘ + myName + ‘, I am thinking of a number between 1 and 20.’)

We are given a while loop in this next block of code. The guessesTaken count was originally set at zero in our first block of code. Now we see the while checks for less than 6 (we are given 6 tries). If less than six, then we are prompted with “Take a guess”. The users guess is put into the “guess” variable. The next line,  guess = int(guess), converts the value to a integer.

while guessesTaken < 6:
print(‘Take a guess.’) # There are four spaces in front of print.
guess = input()
guess = int(guess)

This block of code increases the variable guessesTaken by 1.

guessesTaken = guessesTaken + 1

This block of code does a check to see if the users number (he or she guessed) is lower than the random number value. If so, it prints out the information.

if guess < number:
print(‘Your guess is too low.’) # There are eight spaces in front of print.

This block of code does another check to see if the users number (he or she guessed) is higher than the random number value. If so, it prints out the information.

if guess > number:
print(‘Your guess is too high.’)

This block of code does another check to see if the users number (he or she guessed) is the same as the random number value. If so, it breaks out of the if statement.

if guess == number:
break

Once the above IF is done (either by printing out the information, or breaking out if the value is correct), it does a second check, and if the guess is equal too or correct, will convert the number of guessesTaken back to a string and print out your success and how many tries it took.

if guess == number:
guessesTaken = str(guessesTaken)
print(‘Good job, ‘ + myName + ‘! You guessed my number in ‘ + guessesTaken + ‘ guesses!’)

The final block of code does the same check and if incorrect, will convert the number of tries back to a string and inform you, how many times you tried and you are incorrect with the answer.

if guess != number:
number = str(number)
print(‘Nope. The number I was thinking of was ‘ + number)

 

This is a nice little program, using the random generator function, while loop, if statement, print function, count, and conversion to/from strings and integers. For this week, we will not have a quiz. Rather, I wanted you to see how many small pieces put together, can create a cool little program to play with.

 


Leave a comment

Your email address will not be published. Required fields are marked *