首页 > 解决方案 > 它继续打印

问题描述

当我运行此代码时,它不断给我答案,这是我的代码

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")

标签: pythonpython-2.7calculator

解决方案


您可能想要的是将输入代码放入 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")

推荐阅读