Build a Number Guessing Game in Python
This is a Python tutorial on how to create your own number guessing game in Python. This is actually a game that can be played with a computer with numbers.
The rule of this game:
The computer will choose any random number between 1 to 100. Then the user will try to guess the right number.
If the user failed to enter the random number chosen by the computer then the user will get a hint.
The hints will be like these:
Your guess was low, please enter a higher number
Your guess was high, please enter a lower number
With the help of these hints, you have to find the random number choose by the computer.
When you will enter the right random number chosen by the computer you will get an output like this:
You won!
Number of turns you have used: n
N will be the number of turns the user used to guess the right random number chosen by the computer.
I hope you have understood the rule.
Build a number guessing game in Python
Here is the Python Source code of guess the number game in Python
# guess the number game in Python by CodeSpeedy.com import random random_number = random.randint(1,100) win = False Turns =0 while win==False: Your_guess = input("Enter a number between 1 and 100") Turns +=1 if random_number==int(Your_guess): print("You won!") print("Number of turns you have used: ",Turns) win == True break else: if random_number>int(Your_guess): print("Your Guess was low, Please enter a higher number") else: print("your guess was high, please enter a lower number")
I have played this game and here is its output:
Enter a number between 1 and 100 50 Your Guess was low, Please enter a higher number Enter a number between 1 and 100 75 your guess was high, please enter a lower number Enter a number between 1 and 100 65 your guess was high, please enter a lower number Enter a number between 1 and 100 60 You won! Number of turns you have used: 4 Process finished with exit code 0
Explanation of Number guessing game in Python
import random
This will import the random module in our Python program.
In Python random.randint(1,100) will return a random number in between 1 to 100
Here win is a boolean variable and this is used to check if the user entered the right random number chosen by the computer or not. When the user chooses the random number chosen by the computer the win variable will be set to true
Rest of the program is standing on if else statement to check if the user entered the right random number or not.
You can extend the functionality of this game if you wish.
I can give you some suggestions for that.
- You can create a scoring system using the number of turns
- Also, you can set the limitations for the number of turns that can be used to guess the random number.
Here are some other number guessing game in different programming languages
Guess The Number Game Using Java with Source Code
Guess The Number Game Using JavaScript
You can check the algorithms to extend the features
This code is very bad python code.
1. Incorrectly used equality operator (==) while attempt to assign to the variable win. Variable assignment in python is never done with double equals (==) and in python is done with (=).
2. Python coding standards dictate variable names should be in all lowercase. So you should change Your_guess and Turns to your_guess and turns.
3. The break statement is completely unnecessary (without the earlier error).
4. It doesn’t gracefully handle entering things that can’t be cast to integers. You should wrap the int(your_guess) in a try-except clause that checks for ValueError.
5. Python coding standards state you should put single spaces around comparison operators (== and >).
6. Inconsistent capitalization in messages to users. If the guess is too low then “Your”, “Guess”, and “Please” are capitalized, but are not capitalized if the guess is too high.
Thanks for the Python standard reminding. I am making changes to this code.
Your modification to this code is like this:
import random
random_number = random.randint(1,100)
won = False
turns = 0
while not won:
try:
guess = input(“Enter a number between 1 and 100: “)
guess = int(guess)
except ValueError:
print(“The following is not a valid number: “, guess)
print(“Please try again.”)
continue
turns += 1
if random_number == guess:
print(“You won!”)
print(“Number of turns you have used: “, turns)
won = True
elif random_number > guess:
print(“Your guess was low, please enter a higher number”)
else:
print(“Your guess was high, please enter a lower number”)
#Try to correct it that way, embedding the if command into the try, except, else command, I’m not sure whether you’d like the turns also to be counted if they give a ValueError, I included the case that it will add an +1 turn regardless of ValueError.
won = False
turns = 0
while not won:
guess = input(“Enter a number between 1 and 100: “)
turns += 1
try:
guess = int(guess)
except ValueError:
print(“The following is not a valid number: “, guess)
print(“Please try again.”)
else:
if random_number == guess:
print(“You won!”)
print(“Number of turns you have used: “, turns)
won = True
elif random_number > guess:
print(“Your guess was low, please enter a higher number”)
else:
print(“Your guess was high, please enter a lower number”)