如何让我的随机数猜测游戏再次循环后,用户的输入,以及如何创建一个错误陷阱?

2024-05-14 22:01:07 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我做的随机数猜测程序。它要求用户输入两个数字作为边界,一个高一个低,然后程序将在这两个数字之间选择一个数字。然后,用户必须尝试猜测程序选择的数字。1) 我如何让它询问用户是否愿意再次播放,输入“是”后,程序重新开始,输入“否”后,程序结束?2) 如何创建一个错误陷阱,告诉用户“嘿,你没有输入数字!”并结束程序

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

Tags: oftheanswer程序reyougamenumber
1条回答
网友
1楼 · 发布于 2024-05-14 22:01:07

如果他们猜对了,你首先必须让游戏返回一些东西:

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

然后,您将更新“main”函数,使其包含新的“game”函数:

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

然后,您需要添加一个run_游戏函数来一次运行main多次:

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)

最后,对于错误捕获,您可以执行以下操作:

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

相关问题 更多 >

    热门问题