Chapter 4.7 - The Guessing GAME

4.7 - The Guessing GAME

Copy the following code to Thonny and save it as Guess

# This is a "Guess the number game" # You have 7 attempts to find a number between 1 and 99 # Wisely your numbers choose - Master Yoda's saying # ----------------------------------------------------- import random guesses_taken = 0 # After each attempt this number is increased by 1 print("Hello! What is your name?") my_name = input() number = random.randint(1, 100) print("Well, " + my_name + ", I am thinking of a number between 1 and 99.") # An alternative way to display text using + while guesses_taken < 7: print("Take a guess.") guess = input() # Ask the player for a number guess = int(guess) # Convert text to number guesses_taken = guesses_taken + 1 # Increase the guesses_taken by one if guess < number: print("Your guess is too low.") if guess > number: print("Your guess is too high.") if guess == number: break # Break means get out of while if guess == number: guesses_taken = str(guesses_taken) print("Good job, " + my_name + "! You guessed my number in " + guesses_taken + " guesses!") if guess != number: number = str(number) print("Nope. The number I was thinking of was " + number)


We will play the game together and try to explain the steps using the Debug button.