Skip to main content
 首页 » 编程设计

python之多场比赛后程序不退出

2024年10月01日8artech

from random import random 
 
 
# This function handles the number guessing and number formatting 
def run_game(): 
 
    # rand is declared by grabbing a number between 0 and 1, multiplying it by 100, and rounds to nearest integer 
    rand = round(random() * 100, 0) 
    print("Guess the number [0 - 100]") 
    guesses = 0 
 
    while True: 
 
        # Assigns the 'answer' variable by grabbing user input from console 
        answer = input() 
 
        # Checks if the input from the console is a number, and if not, asks the user to enter a valid number 
        if answer.isdigit(): 
            n = int(answer) 
            if n > int(rand): 
                print("Number is less than " + str(n)) 
                guesses = guesses + 1 
            elif n < int(rand): 
                print("Number is greater than " + str(n)) 
                guesses = guesses + 1 
            else: 
                guesses = guesses + 1 
                print("It took you " + str(guesses) + " guesses to guess the right number!") 
                reply = play_again() 
                if reply is False: 
                    break 
                else: 
                    run_game() 
        else: 
            print("Please enter a number") 
 
 
def play_again(): 
    while True: 
        reply = input("Play again? (y/n)\n") 
        if reply.lower() == "y": 
            return True 
        elif reply.lower() == "n": 
            return False 
        else: 
            print("Enter 'y' or 'n'") 
 
 
if __name__ == "__main__": 
    run_game() 

所以当我运行这个程序时,它运行良好。猜中数字后,我可以输入 y 或 n 再次玩。如果我只玩过一次,效果很好。但是如果我选择 y,然后再玩,玩完第二局后输入 n 没有任何作用

请您参考如下方法:

您的主要问题是您正在使用递归开始新游戏,但在递归调用返回后(假设它返回),您只是继续原来的游戏。

有几种方法可以解决这个问题。最简单的方法是更改​​处理检查用户选择再次播放的代码,以便它始终 breaks:

if reply: 
    run_game() 
break 

更好的方法是摆脱递归。有几种方法可以做到这一点。一个简单的想法是简单地重置适当的变量,并在用户想要再次玩游戏时继续游戏循环:

reply = play_again() 
if reply: 
    rand = round(random() * 100, 0) 
    print("Guess the number [0 - 100]") 
    guesses = 0 
else: 
    break 

另一种避免递归的方法是添加另一个循环。这是您可以使用单独的函数执行此操作的一种方法:

def run_game(): 
    rand = round(random() * 100, 0) 
    print("Guess the number [0 - 100]") 
    guesses = 0 
 
    while True: 
        answer = input() 
        if answer.isdigit(): 
            n = int(answer) 
            if n > int(rand): 
                print("Number is less than " + str(n)) 
                guesses = guesses + 1 
            elif n < int(rand): 
                print("Number is greater than " + str(n)) 
                guesses = guesses + 1 
            else: 
                guesses = guesses + 1 
                print("It took you " + str(guesses) + " guesses to guess the right number!") 
                break # unconditionally break here! 
 
def run_many_games(): 
    again = True 
    while again: 
        run_game() 
        again = play_again() 

您可能会注意到我在上面的所有代码中所做的更改是我如何测试 play_again 的返回值是 True 还是 False 。当您已经有一个 bool 值时,不需要额外的比较步骤。只需执行 if reply(或者 if not reply 如果您正在测试 False)。您也可以在 while 循环条件下执行此操作,就像我在上一个代码块中使用 again 所做的那样。