Technology

Programming

Post Page Advertisement [Top]

Programming Languages

Python - Random number guessing game

Make a program which randomly chooses a number to guess and then the user will have a few chances to guess the number correctly. In each wrong attempt, the computer will give a hint that the number is greater or smaller than the one you have guessed.




# Any random number guessing game
import random
# Generating random number between 1 to 101
random_num = random.randint(1,101)
status = True
# While loop will run until status = True
while status:
    guess_num = int(input(("Guess a number: ")))
    if guess_num == random_num:
        print("Congratulations, You won! ")
        status = False
    else:
        if guess_num < random_num:
            print("The number is greater than the one you have guessed.")
        else:
            print("The number is smaller than the one you have guessed.")

# Techhighland™

                                                                                           
  OUTPUT                                                                                         

Guess a number: 70
The number is greater than the one you have guessed.
Guess a number: 84
The number is smaller than the one you have guessed.
Guess a number: 75
The number is smaller than the one you have guessed.
Guess a number: 72
The number is greater than the one you have guessed.
Guess a number: 73
Congratulations, You won! 

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib