Here are the high-level steps you will need.
- Generate a random number and store it
- Get input from the user and store it as the guess
- Compare the guess to the generated number
- Display the determination ( eg. 'go higher or go lower' ) on the screen
- Loop until the correct number is guessed
- Bonus:
- Add comedic comments to the code for when the player does something wrong - keep it nice!! - nothing rude
- Write the game in reverse - You think of a number and have the game guess your number
import random
print('Hi, welcome to the game. This is a number guessing game.\nYou got 7 chances to guess my number between 1 and 100. Let us begin!')
number_to_guess = random.randrange(100)
chances = 7
guess_counter = 0
while guess_counter < chances:
guess_counter += 1
my_guess = int(input('Please Enter your Guess : '))
if my_guess == number_to_guess:
print(f'The number is {number_to_guess} and you guessed right in {guess_counter} attempts!!')
break
elif guess_counter >= chances and my_guess != number_to_guess:
print(f'Oops sorry, The number is {number_to_guess} better luck next time.')
elif my_guess > number_to_guess:
print('Try again, but guess lower.')
elif my_guess < number_to_guess:
print('Try again, but guess higher.')
After you get it working, try raising the top number ( a.k.a. the 'ceiling' ) and play again.
Want to have more fun? Try adding some silly comments when the user make a mistake, like your last guess was 77 and it told you to guess lower. However, instead, you guess higher. Keep it clean, though!!
Bonus round: Have the program guess your number while you tell it 'higher' or 'lower'.