We are going to build guess number game using the while loop.
There we will generate a random number to save as a secret number then we will ask them to user guess if the user's number matches our secret number then the user will win or else the user will lose the game.
Rules of Game:
- User can guess a number max 5 times or else the user will lose the game
- If users guess a high number then we will tell them you are high
- If users guess a low number then we will tell them you are low
- If the user has lost the game then at the end we will tell GAME OVER
Here is the code 👇
import random
secret_number = random.randint(1, 20)
i = 1
while i <= 5:
user = int(input("Enter your guess number\n"))
if user == secret_number:
print("Correct Number")
break
elif user > secret_number:
print("To High")
elif user < secret_number:
print("To Low")
if i == 5:
print("GAME OVER")
i += 1
Top comments (0)