Situatie
A number guessing game aims to guess the number that the program has come up with.
- The program randomly selects a number between 1 and 100
- It will then ask the player to enter his proposal.
- It will then check if this number is the same as the one generated randomly
- If the player’s guess is not the same, then he will check if the number is higher or lower than the guess and tell the player.
Backup
# to import random module
import random
# to create a range of random numbers between 1-10
n = random.randrange(1,100)
# to take a user input to enter a number
guess = int(input("Enter any number: "))
while n!= guess: # means if n is not equal to the input guess
# if guess is smaller than n
if guess < n:
print("Too low")
# to again ask for input
guess = int(input("Enter number again: "))
# if guess is greater than n
elif guess > n:
print("Too high!")
# to again ask for the user input
guess = int(input("Enter number again: "))
# if guess gets equals to n terminate the while loop
else:
break
print("you guessed it right!!")
Solutie
Output:
Enter any number: 67
Too high!
Enter number again: 45
Too low
Enter number again: 54
you guessed it right!!
Leave A Comment?