我有这个单词解扰器游戏,仅在 CMD 或 python shell 中运行。当用户正确或错误地猜测单词时,它会显示“按任意键再次播放”
我怎样才能让它重新开始?
在评估用户的输入后不要让程序退出;相反,在循环中执行此操作。例如,一个甚至不使用函数的简单示例:
phrase = "hello, world"
while input("Guess the phrase: ") != phrase:
print("Incorrect.") # Evaluate the input here
print("Correct") # If the user is successful
这会输出以下内容,并显示我的用户输入:
Guess the phrase: a guess
Incorrect.
Guess the phrase: another guess
Incorrect.
Guess the phrase: hello, world
Correct
这显然很简单,但逻辑听起来就像你所追求的。稍微复杂一点的版本,带有定义的函数,供您查看逻辑适合的位置,可能如下所示:
def game(phrase_to_guess):
return input("Guess the phrase: ") == phrase_to_guess
def main():
phrase = "hello, world"
while not game(phrase):
print("Incorrect.")
print("Correct")
main()
输出是相同的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)