Untitled

mail@pastecode.io avatar
unknown
python
a month ago
843 B
2
Indexable
Never
import random

def guess_the_number():
    number_to_guess = random.randint(1, 100)
    
    print("Welcome to Guess the Number!")
    
    while True:
        guess = input("Please guess a number between 1 and 100: ")
        
        try:
            guess = int(guess)
        except ValueError:
            print("Please enter a valid number.")
            continue
        
        if guess < number_to_guess:
            print("Too low. Try again.")
        elif guess > number_to_guess:
            print("Too high. Try again.")
        else:
            print("Congratulations! You guessed the number.")
            play_again = input("Would you like to play again? (y/n): ")
            if play_again.lower() == "y":
                number_to_guess = random.randint(1, 100)
            else:
                break

guess_the_number()
Leave a Comment