While循环在满足条件后继续运行



我正在尝试使用Python制作一个简单的计算器。

我希望用户写& & &,然后while循环结束。当我运行它并输入随机文本时,while循环工作并说"重试"。然而,当我实际输入正确的答案("add")时,while循环并没有结束-相反,它继续说"try again"

为什么会发生这种情况?我的代码有什么问题?

input('Welcome! This is a calculator app that allows you to perform basic arithmetic operations with whole and decimal numbers. Press Enter to continue.')
operation = input('Please pick one of the following operations: Multiply, Add, Divide or Subtract ')
while True:
if operation != 'add':
input('Please try again: ')
else:
print('All good')
break

看这里:

if operation != 'add':
input('Please try again:')

这段代码只允许用户输入一些东西,但是在哪里呢?我们需要输入操作值

if operation != 'add':
operation = input('Please try again:')

最新更新