它正在继续打印

it is keep on printing

当我 运行 这段代码时,它一直在给我答案 这是我的代码

num1 = float(raw_input("enter a number: "))  # type: float
operation = str(raw_input("enter a operation: "))
num2 = float(raw_input("enter a number: "))  # type: float

  while True:
        if operation == "+":
           print num1 + num2
        elif operation == "-":
           print num1 - num2
        elif operation == "*":
           print num1 * num2
        elif operation == "/":
           print (num1 / num2)
        else:
           print("Error Error")

您可能想要将输入获取代码放入 while 循环中:

while True:
    num1 = float(raw_input("enter a number: "))  # type: float
    operation = str(raw_input("enter a operation: "))
    num2 = float(raw_input("enter a number: "))  # type: float
    
    if operation == "+":
        print (num1 + num2)
    elif operation == "-":
        print (num1 - num2)
    elif operation == "*":
        print (num1 * num2)
    elif operation == "/":
        print (num1 / num2)
    else:
        print("Error Error")
`while True:` means Infinite Loop.

您可以在 while 循环中获取输入,或者您可以更改 while 循环的条件。

删除while True:,它只会打印一次答案。 while 循环继续 运行 只要参数为真且 True 始终为真:P

您可能希望应用程序继续计算用户的输入。

试试这个

def calculate():
    num1 = float(raw_input("enter a number: "))  # type: float
    operation = str(raw_input("enter a operation: "))
    num2 = float(raw_input("enter a number: "))  # type: float
    
    if operation == "+":
        print (num1 + num2)
    elif operation == "-":
        print (num1 - num2)
    elif operation == "*":
        print (num1 * num2)
    elif operation == "/":
        print (num1 / num2)
    else:
        print("Error Error")

while True:
    calculate()