gpt4 book ai didi

python - 您如何根据给定的用户输入让程序再次运行?

转载 作者:行者123 更新时间:2023-12-04 09:09:22 25 4
gpt4 key购买 nike

我为一个简单的猜谜游戏制作了这段代码,其中包括输入一个猜数字的输入(由变量 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 .更强大的是采用用户根本无法输入的数字,例如无穷大:import math; guessing_games = math.inf .
现在所有条件都可以使用 while 循环。顾名思义,它是关于一遍又一遍地执行同一个块。为了防止程序无限运行,我们需要一个终止条件。这在您的情况下很清楚:当用户猜到正确的数字时,块的执行应该停止。
这意味着块应该在 while 循环中运行,直到不再满足循环的条件:while guessing_games != guess_number .当然重要的是变量guessing_games可以在块内更改。这是因为在块执行开始时总是要求用户输入新数字。
变体 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陈述
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 向您展示了避免初始化 guessing_games 的可能性。使用 while True (否则如变体 1 和 3 所示,必须进行初始化以避免错误: NameError: name 'guessing_games' is not defined )。但是你必须小心,循环肯定会在某个时候结束。
变体 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com