DEV Community

Arunesh kumar
Arunesh kumar

Posted on

Guess The Number Game in Python

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:

  1. User can guess a number max 5 times or else the user will lose the game
  2. If users guess a high number then we will tell them you are high
  3. If users guess a low number then we will tell them you are low
  4. 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

Enter fullscreen mode Exit fullscreen mode

Top comments (0)