简体   繁体   中英

How to get my random number guessing game to loop again upon user input and how to create an error trap?

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()

You'd first have to make game return something if they guess right:

def game(answer):
    guess = int(input("Please put in your number, then press enter:\n"))
    if answer > guess:
        print("Too big")
        return False
    if answer < guess:
        print("Too small")
        return False
    elif answer == guess:
        print("Your guess is just right")
        return True

Then, you'd update the 'main' function, so that it incorporates the new 'game' function:

def main():
    c = int(input("How many guesses would you like?\n"))
    for i in range(c):
        answer = int(input("Your guess: "))
        is_right = game(answer)
        if is_right: break
    if is_right: return True
    else: return False

Then, you'd add a run_game function to run main more than once at a time:

def run_game():
    introduction()
    not_done = False
    while not_done:
        game()
        again = input('If you would like to play again, please type any character')
        not_done = bool(again)

Finally, for error catching, you'd do something like this:

try:
    x = int(input())
except:
    print('That was not a number')
    import sys
    sys.exit(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM