Build a Guessing Game in Python
Build a point based guessing game in python.
Introduction
Hey guys, we will be building a python guessing game, but not your regular guessing game. In this tutorial, we will build a guessing game with the following features
A User has 3 guesses
We keep track of the user's score
The application runs till the user decides to quit. For each random number generated, the user has 3 guesses
Each guess has a score point e.g The first guess gets 5 points, second guess gets 3 points and the last guess get 1 point.
Let's Get Started
Building the Guessing Game
We start by building the regular guessing game and then add the other features.
# import the random module
import random
print("\n\nStart Game \n")
random_number = random.randint(1,5)
# Use a for loop because we want it to run 3 times
for i in range(0, 3):
user_guess = int(input("Enter your guess: "))
if user_guess == random_number :
print("You won")
break
else:
print("Try Again")
Explanation
import random
- This line imports the random number module into our programrandom_number = random.randint(1,5)
- This line generates a random number between 1 and 5, stores the value in the random_number variable. We can compare the user guess to this random_number value later on.user_guess = int(input("Enter your guess"))
- Takes the user input and converts it to an integer if it can't convert the value to a number, it throws a runtime error.if user_guess == random_number
- checks if the guess is equal to our saved random number, if yes, it prints you won, then ends our for loop with the break statement or it tells the user to try again, till the end of the loop
We generate a random number, store the value of this random number, have the for loop run 3 times, and for each time en we get the user input and compare it to our random number, and either end the game if the guess is correct or ask the user to try again
Making it run till infinity
import random
# Use a while loop to create an infinite loop
while True:
# Use a for loop because we want it to run 3 times
print("\n\nStart Game \n")
random_number = random.randint(1,5)
for i in range(0, 3):
user_guess = int(input("Enter your guess: "))
if user_guess == random_number :
print("You won")
break
else:
print("Try Again")
user_answer = input("Do you want to play again Y/N: ")
if user_answer == 'N' or user_answer == 'n':
break
Explanation
So we added four new lines
while True:
- This creates an infinite loop, i.e a loop that runs FOREVER. we need this because we want our game to keep running until the user decides to quit. we put the random number inside the loop as well because we want the number to be different each time the player restarts the game.user_answer = input("Do you want to play again Y/N: ")
- asks the user if they wish to play another round.if user_answer == 'N' or user_answer == 'n':
- checks the users reply, if they entered a 'N' or 'n' then the program ends because of the break statement. It keeps running if they enter anything else.
The while loop creates an infinite loop, we therefore need a way to break out of this loop. At the end of each round the user decides if they wish to play again or not. This way, we're sure our program ends when the user is tired of playing
Adding a Scoring System
Now we're going to keep track of the points our player gets. Here is the rule for the points, guessing right on the first try is 5 points, on the second try 3 points and on the last try is 1 point.
# import the random module
import random
score = 0
game_played = 0
# Use a while loop to create an infinite loop
while True:
# Use a for loop because we want it to run 3 times
print("\n\nStart Game \n")
game_played += 1
random_number = random.randint(1,5)
for i in range(0, 3):
user_guess = int(input("Enter your guess: "))
if user_guess == random_number :
print("You won")
if i == 0 :
score += 5
if i == 1 :
score += 3
if i == 2 :
score += 1
break
else:
print("Try Again")
user_answer = input("Do you want to play again Y/N: ")
if user_answer == 'N' or user_answer == 'n':
break
print("You scored {} \n".format(score))
print("You played {} game(s)".format(game_played))
Explanation
- We add the score and game_played variables to keep track of the player's score and the number of times the player has played the game. We also keep this outside of the loop because we don't want to reset the value of these variable each time the game restarts.
game_played += 1
- increases the game_played variable by 1 every time the loop starts over.if i == 0
- checks if the user is on the first guess, since we make use of a for loop with range(0,3), the first guess i == 0, second guess i == 1, and the third guess i == 2. With this in mind we can give the appropriate points depending on the value of i. And notice we did this inside the condition where the user's guess is equal to the random number, we only want to increase the score if the user's guess is correctprint("You scored {}".format(score))
- prints the score to the user at the end, when the user decides to quit the game.print("You played {} game(s)".format(game_played))
- prints the total number of games the user played.
Conclusion
The way we get better as programmers is by practice. Here are some more challenges/improvements you can add to this program to improve your python skills
- Add a save to file functionality, where at the end of the program, the game saves the user's score and at the end of each game it displays a high score.
- Ask the user for a name, and at the end of the game, save the name of the user and their score to a file.