这是我到目前为止的函数代码(可能发生错误的地方):
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
我试图做到这一点,以便我可以立即输入另一个数字,并不间断地添加一个又一个数字。然而,由于某种原因,循环只是重复。即使我在继续提示中输入“否”,它甚至不会中断。 “是”和“否”都会产生完全相同的结果,但我不明白。
您是否尝试过使用布尔变量而不是分解 2 个级别?
即
def showAddition():
print("You have selected: \"Addition\"")
continueProcess = True
while continueProcess:
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:
continueProcess = False