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就什么都不做
答案 0 :(得分:2)
你的主要问题是你正在使用递归来开始新游戏,但是在递归调用返回之后(假设确实如此),你只需继续进行原始游戏。
有几种方法可以解决这个问题。最简单的方法是更改处理检查用户选择再次播放的代码,以便始终break
s:
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
一样。
答案 1 :(得分:0)
这是解决这个问题的好方法。在你的代码中,你永远不会真正退出while循环,因为运行游戏永远不会退出,并且没有返回系统变量来破坏它。使用sys.exit(0)也有效,但这是一个习惯于进入这些类型程序的习惯。
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:
answer = input()
if type(answer) == int:
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
reply = play_again()
if reply:
run_game()
else:
print 'Thank you for playing'
def play_again():
while True:
reply = raw_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()
答案 2 :(得分:0)
发生这种情况的原因是因为run_game
最终以递归方式调用自身。当用户选择再次播放时,它不是重新开始游戏,而是有效地创建了游戏的新实例。然后,当用户选择停止播放时,它返回旧会话而不是退出程序。
您甚至可以通过在选择再次播放之前记住解决方案,然后选择在第二次会话后不再播放来证明这一点。然后,您将再次播放上一个会话并输入您记住或记下的解决方案。
现在,您可以使用sys.exit()
代替break
来强制程序关闭来解决此问题,但这似乎不是一种好习惯。如果有人选择再次播放太多次,他们可能会导致程序耗尽堆栈空间并崩溃。相反,最好将这个支票移出run_game
这样
if __name__ == "__main__":
while True:
run_game()
if not play_again():
break
并将run_game
中的else块修改为此
else:
guesses = guesses + 1
print("It took you " + str(guesses) + " guesses to guess the right number!")
break
答案 3 :(得分:0)
根据用户输入返回True或False是没有意义的,你可以直接在那里工作。
import sys
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!")
play_again()
else:
print("Please enter a number")
def play_again():
while True:
reply = input("Play again? (y/n)\n")
if reply.lower() == "y":
run_game()
elif reply.lower() == "n":
sys.exit(0)
else:
print("Enter 'y' or 'n'")
if __name__ == "__main__":
run_game()
这样代码更清晰,可以解决您的问题。当你可以直接做事时,传递“旗帜”毫无意义。
由于您的游戏从0到100,您还应该验证用户是否没有输入大于100的数字,因为低于0的数字不会通过isdigit
检查。