如果语句没有按预期跳出循环
这是我目前的函数代码(错误可能发生在这里):
def showAddition():
print("You have selected: \"Addition\"")
while True:
last_answer = None
if last_answer:
next_num = int(input("Please input another number: "))
answer = calc.addNums(last_answer, next_num)
else:
num1 = int(input("Please enter your first number: "))
num2 = int(input("Please enter your second number: "))
answer = calc.addNums(num1, num2)
print(f'Your answer is {answer}')
continue_prompt = input("Would you like to continue? Y/N\n")
if continue_prompt.upper() == "Y" or continue_prompt.upper() == "YES":
last_answer = answer
else:
break
我想让它可以让我立即输入另一个数字,能够一个接一个地加数字,不会出错。但是,不知道为什么,循环就是一直重复。即使我在继续提示中输入“no”,它也不会停止。无论是输入“yes”还是“no”,结果都是完全一样的,我搞不懂这是为什么。
4 个回答
0
如果你假设在询问是否继续时,除了“N”以外的任何回答都意味着这个过程应该继续,那么你可以简化这个过程。
一定要验证数字输入。
举个例子:
class calc:
@staticmethod
def addNums(*args):
return sum(args)
def proceed() -> bool:
while True:
yn = input("Do you wish to continue Y/N: ").upper()
if yn in {"Y", "N"}:
return yn == "Y"
print("\aPlease respond with Y or N")
def getint(prompt: str) -> int:
while True:
x = input(prompt)
try:
return int(x)
except ValueError:
print(f"\a'{x}' is not a valid integer")
def showAddition():
print("You have selected: \"Addition\"")
total = getint("Please enter your first number: ")
while True:
num = getint("Please enter the next value: ")
total = calc.addNums(total, num)
print(f"Current {total=}")
if not proceed():
break
0
除了Razumov建议的调整缩进外,还要注意“输入另一个数字”的那部分代码永远不会执行,因为每次循环开始时,last_answer
都会被重置为None。我们只在一开始的时候把它设为None就可以了……
如果你想在循环中报告一个累加的总和,可以试试这个:
class calc:
@staticmethod
def addNums(*args):
return sum(args)
def showAddition():
print("You have selected: \"Addition\"")
last_answer = None
while True:
if last_answer:
next_num = int(input("Please input another number: "))
answer = calc.addNums(last_answer, next_num)
else:
num1 = int(input("Please enter your first number: "))
num2 = int(input("Please enter your second number: "))
answer = calc.addNums(num1, num2)
print(f'Your answer is {answer}')
continue_prompt = input("Would you like to continue? Y/N\n")
if continue_prompt.upper() == "Y" or continue_prompt.upper() == "YES":
last_answer = answer
else:
break
(你没有提供calc.addNums
的具体实现,我加了一个实现,这样我才能运行代码。)
0
def showAddition():
print("You have selected: \"Addition\"")
# store the result of the previous addition
last_answer = None
# to stop the loop
continue_adding = True
while continue_adding:
if last_answer:
next_num = int(input("Please input another number: "))
# answer = last_answer + next_num
answer = calc.addNums(last_answer, next_num)
print(f'Your answer is {answer}')
# store new value in last answer
last_answer = answer
else:
num1 = int(input("Please enter your first number: "))
num2 = int(input("Please enter your second number: "))
# answer = num1 + num2
answer = calc.addNums(num1, num2)
print(f'Your answer is {answer}')
# check input
while True:
continue_prompt = input("Would you like to continue? Y/N\n")
# continue addition directly with stored last_answer
if continue_prompt.upper() in {"Y","YES"}:
last_answer = answer
break
elif continue_prompt.upper() in {"N","NO"}:
# stop the loop
continue_adding = False
break
else:
# ask again to only input Y/N
print("Only Enter Either Y/N")
continue
以下是一些编程建议,帮助你更好地理解如何处理循环和条件判断:
- 把上一个答案存储到循环外面的一个变量里,这样在下次循环时就不会被重置了。
- 设置一个循环变量叫做
continue_loop
,用来控制循环的停止,这样当输入不是Y
或N
时,就不用写太多代码来处理。 - 与其使用
if x == y
这样的比较,不如使用集合或列表来查找,比如if x in [x,y,z]
。