Machine Problem 1
Number Guessing Game
In the number guessing game, the computer will randomly select a number from a range of numbers e.g. 1-100 and then the game will give the player five attempts to guess the number.
Gameplay:
• If the player's guess is higher than the chosen number, the program will tell the player to try another LOWER than the previous guess.
• If the player's guess is lower than the chosen number, the program will tell the player to try another number HIGHER than the previous guess.
■ If the player guesses the number before he consumes the five attempts, then display congratulatory message.
import random
print ("welcome to the guess my number ")
print ("you have 5 guesses\n")
the_number = random.randint(1, 100)
user = int(input("Guess the number?"))
count = 1
while user != the_number:
if user > the_number:
print ("Lower")
elif user < the_number:
print ("Higher")
else :
print ("congratulations")
user = int(input("What's the number?"))
count += 1
if count == 5:
break
print(" the number is", the_number)
input ("\nPress enter to exit")
Comments
Leave a comment