What number am I thinking of?
★★☆A simple guessing game for young children asks the child to guess the number the computer has chosen at random. The child keeps guessing until they guess correctly.
Write a program that generate a random number and asks the user to guess the number, outputting whether the guess is correct, too high or too low. The program should also output the number of guesses taken once the number has been guessed correctly.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
play_guess_the_number that:What is the lowest number I can choose? :1
What is the highest number I can choose? :10
OK, let's play. How many guesses will you take?
Enter the number I'm thinking of: 5
Your guess is too low.
Enter the number I'm thinking of: 7
You've got it, I chose 7. It took you 2 guesses.
# Guess the number program
# -------------------------
# Import libraries
# -------------------------
import random
# -------------------------
# Subprograms
# -------------------------
# Routine to play the guess the number game
def play_guess_the_number(min, max):
---
# Initialise variables
random.seed()
cpu = random.randint(min, max)
player_guess = 0
guess_count = 0
---
# Continue playing until player wins
while player_guess != cpu:
---
player_guess = int(input("Enter the number I'm thinking of: "))
guess_count = guess_count + 1
---
# Check if guess is too high or too low
if player_guess < cpu:
---
print("Your guess is too low.")
---
elif player_guess > cpu:
---
print("Your guess is too high.")
---
print("You've got it, I chose {0}. It took you {1} guesses.".format(cpu, guess_count))
---
# -------------------------
# Main program
# -------------------------
---
min = int(input("What is the lowest number I can choose? :"))
max = int(input("What is the highest number I can choose? :"))
---
print("OK, let's play. How many guesses will you take?")
play_guess_the_number(min, max)