[Python] How to get my random number guessing game to loop again upon user input and how to...

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    So this is my random number guessing program I made. It asks the user to input two numbers as the bound, one high and one low, then the program will choose a number between those two. The user then has to try and guess the number chosen by the program. 1) How do I get it to ask the user if they would like to play again and upon inputting 'yes' the program starts over, and inputting 'no' the program ends? 2) How do I create an error trap that tells the user "Hey you didn't enter a number!" and ends the program?

    def main(): # Main Module

    print("Game Over.")

    def introduction():
    print("Let's play the 'COLD, COLD, HOT!' game.")
    print("Here's how it works. You're going to choose two numbers: one small, one big. Once you do that, I'll choose a random number in between those two.")
    print("The goal of this game is to guess the number I'm thinking of. If you guess right, then you're HOT ON THE MONEY. If you keep guessing wrong, than you're ICE COLD. Ready? Then let's play!")
    small = int(input("Enter your smaller number: "))
    large = int(input("Enter your bigger number: "))
    print("\n")

    return small, large

    def game(answer):

    c = int(input('Input the number of guesses you want: '))

    counter = 1 # Set the value of the counter outside loop.

    while counter <= c:
    guess = int(input("Input your guess(number) and press the 'Enter' key: "))
    if answer > guess:
    print("Your guess is too small; you're ICE COLD!")
    counter = counter + 1
    elif answer < guess:
    print("Your guess is too large; you're still ICE COLD!")
    counter = counter + 1
    elif answer == guess:
    print("Your guess is just right; you're HOT ON THE MONEY!")
    counter = c + 0.5
    if (answer == guess) and (counter < c + 1):
    print("You were burning hot this round!")
    else:
    print("Wow, you were frozen solid this time around.", "The number I \
    was thinking of was: " , answer)

    def Mystery_Number(a,b):

    import random

    Mystery_Number = random.randint(a,b) # Random integer from Python

    return Mystery_Number # This function returns a random number

    A,B = introduction()

    number = Mystery_Number(A,B) # Calling Mystery_Number

    game(number) # Number is the argument for the game function

    main()

    Continue reading...

Compartilhe esta Página