我为一个简单的猜谜游戏编写了这段代码,其中涉及输入一个输入来猜测数字(由变量guess_number 定义)。我想要它做的是,如果输入不正确,则再次运行,并在答案正确时停止//终止,但我无法得到它。我假设我需要一个 while 循环,但我不太理解它(一周前开始编程)。这是基本程序:
guess_number = 33
guessing_games = int(input('Try to guess the number I am thinking of, 1-50: '))
if guessing_games == guess_number:
print("Great job! You got the number correct!")
elif guessing_games > guess_number:
print("Too high! Guess again.")
elif guessing_games < guess_number:
print("Too low! Guess again.")
最佳答案
在其他编程语言中,您可以使用 do-while 语句来完成此任务,即您可以首先向用户询问一个数字,然后执行代码,直到用户给出正确的数字。 Python中没有这样的声明。不过没关系,您还有其他几种方法可以使代码正常工作。
所有这些可能性(除了变体 2!)的共同点是您已经需要初始化一个值来检查 while 循环中的条件。对于您的示例(用户显然可以输入 1 到 50 之间的数字),只需选择 0 就足够了,例如guessing_games = 0
。更稳健的方法是采用用户根本无法输入的数字,例如无穷大:导入数学; guessing_games = math.inf
.
现在所有条件都给出使用 while 循环。顾名思义,它是一遍又一遍地执行同一个 block 。为了防止程序无限运行,我们需要一个终止条件。在您的情况下这一点很明显:当用户猜到正确的数字时, block 的执行应该停止。
这意味着该 block 应在 while 循环内运行,直到不再满足循环条件:whileguessing_games !=guess_number
。当然,变量 guessing_games
可以在 block 内更改,这一点很重要。出现这种情况是因为在 block 执行开始时总是要求用户输入新数字。
变体 1:使用简单的终止条件并在 while 循环后打印成功消息
import math
guess_number = 33
guessing_games = math.inf
while guessing_games != guess_number:
guessing_games = int(input("Try to guess the number I am thinking of, 1-50: "))
if guessing_games > guess_number:
print("Too high! Guess again.")
elif guessing_games < guess_number:
print("Too low! Guess again.")
print("Great job! You got the number correct!")
变体 2:通过 break
语句在 block 内使用显式终止条件
guess_number = 33
while True:
guessing_games = int(input("Try to guess the number I am thinking of, 1-50: "))
if guessing_games > guess_number:
print("Too high! Guess again.")
elif guessing_games < guess_number:
print("Too low! Guess again.")
else:
print("Great job! You got the number correct!")
break
变体 2 向您展示了使用 while True
避免初始化 guessing_games
的可能性(否则如变体 1 和 3 所示,必须进行初始化以避免错误:NameError:名称“guessing_games”未定义
)。但你必须小心,循环肯定会在某个时刻结束。
变体 3:与变体 1 非常相似,不同之处在于成功消息可以被视为 while 循环的一部分。
import math
guess_number = 33
guessing_games = math.inf
while guessing_games != guess_number:
guessing_games = int(input("Try to guess the number I am thinking of, 1-50: "))
if guessing_games > guess_number:
print("Too high! Guess again.")
elif guessing_games < guess_number:
print("Too low! Guess again.")
else:
print("Great job! You got the number correct!")
另请参阅Python documentation :
The while statement is used for repeated execution as long as an expression is true:
while_stmt ::= "while" assignment_expression ":" suite ["else" ":" suite]
This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.
A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.
我实际上建议的最后一件事是检查用户是否输入了数字:
try:
guessing_games = int(input("Try to guess the number I am thinking of, 1-50: "))
except ValueError:
print("Only numbers are allowed.")
continue
关于python - 如何让程序根据给定的用户输入再次运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63386683/