Skip to main content

Python Small Game – Guess The Right Number

Submitted by admin on Sun, 12/12/2021 - 11:29

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.

# 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")
        print("Sorry wrong number. Please try again!!")
    guess_count = guess_count + 1
    print(f"Sorry, You have guesses maximum allowed times. The correct number is {right_number}")


Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.