Python Small Game – Guess The Right Number

python-game-guess-number

In this python tutorial, we will create a small game. User will be asked to guess a single digit number (0-9). User will be allowed to guess maximum 3 times.

If the guessed number is correct, it will show congratulation message. If user failed to guess the right number in maximum 3 tries, he will get message that he has guessed maximum times and will also show the right number.

Every time, the game is played, it will generate a random correct number.

So, here is the complete code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# import random module
import random

right_number = random.randint(0,9)
guess_limit = 3
guess_count = 1
while guess_count <= guess_limit:
    guess_number = int(input("Guess the right number: "))
    if (guess_number == right_number ):
        print("Congratulations!!! You have guessed the right number")
        break
    else:
        print("Sorry wrong number. Please try again!!")
    guess_count = guess_count + 1
else:
    print(f"Sorry, You have guesses maximum allowed times. The correct number is {right_number}")

Related Posts

Leave a Reply

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