Unit 2 Task 46 - Solution

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

Solution - Task 46: Guess my number with play again

← Back to task


One possible program:

import random

play_again = "y"
while play_again.lower() == "y":
    secret = random.randint(1, 20)
    print("New game! 1-20.")
    while True:
        player_guess = int(input("Guess: "))
        if player_guess < secret:
            print("Too low")
        elif player_guess > secret:
            print("Too high")
        else:
            print("Got it!")
            break
    play_again = input("Play again? (y/n): ")