所以我正在制作我的简单计算器,这就是我到目前为止所拥有的:
import time
print ("Welcome. This is a calculator that uses the function: A (operator) B.")
time.sleep(3.5)
print ("Available operators include: Addition, Subtraction, Multiplication, Division, Exponent, and Remainder division.")
time.sleep(3.5)
while True:
a = float(input("Type in a value of A. "))
b = float(input("Type in a value of B. "))
opera = input("Would you like to: Add - Subtract - Multiply - Divide - Exponent - or Remainder? ")
opera = opera.lower()
while not (opera) == "add" or (opera) == "subtract" or (opera) == "multiply" or (opera) == "divide" or (opera) == "exponent" or (opera) == "remainder":
print ("Invalid operation.")
opera = input("Would you like to: Add - Subtract - Multiply - Divide - Exponent - or Remainder? ")
break
if (opera) == "add":
print ((a) + (b))
if (opera) == "subtract":
print ((a) - (b))
if (opera) == "multiply":
print ((a) * (b))
if (opera) == "divide":
print ((a) / (b))
if (opera) == "exponent":
print ((a) ** (b))
if (opera) == "remainder":
print ((a) % (b))
cont = input("Would you like to do another problem?")
cont = cont.lower()
if cont != "yes":
break
quit
因此,当我启动计算器并输入 a 和 b 的值时,输入除 add 之外的任何内容都会导致无效操作。然后它会提示我进行有效操作,然后它适用于所有操作。我该如何解决?我假设问题与 while 不在 while true 内有关。