{{tag>projects cloud club computing virtualization machines VMs AWS Azure GCP}} [[python_club|About the Club]]\\ ==== Python Club Topics - Exercise: Guess my number game ==== ==== Exercise: Guess my number game ==== - Output: Create a game where the computer will generate a random number between 1 and 100. Ask the player to enter a guess. Evaultate the guess and determine if the next guess should be higher or lower. Display your determination to the player. The player will continue to guess until the correct value is determined. - What you learn from the example: - Using all of the components you have learned to create an interactive game - Create functions - Use the random module - Use conditionals - Create a menu - Get and interpret input from the user ==== Hints ==== 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 - ~~codedoc: 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 \\ ==== Solution ==== 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'.