我试图在蟒蛇一个简单的计算器,但我似乎得到的输入错误,每当我尝试运行它:输入错误
while True:
print("please choose one of these available options:\n\"add, subtract, multiply, divide, quit\"\ntype in exactly as shown or the calculator won't work");
choice = input();
if(choice == "quit"):
break;
elif(choice == "divide"):
num1 = float(input("Please enter a number:"));
num2 = float(input("Please enter another number:"));
output = num1/num2;
print("The answer is:", output);
elif(choice == "multiply"):
num1 = float(input("Please enter a number:"));
num2 = float(input("Please enter another number:"));
output = num1*num2;
print("The answer is:", output);
elif(choice == "subtract"):
num1 = float(input("Please enter a number:"));
num2 = float(input("Please enter another number:"));
output = num1-num2;
print("The answer is:", output);
elif(choice == "add"):
num1 = float(input("Please enter a number:"));
num2 = float(input("Please enter another number:"));
output = num1+num2;
print("The answer is:", output);
else:
print("Invalid Input. Try again");
当我运行代码,解释只是表明:NameError: name '...' is not defined
每当我输入内容时(用输入替换省略号)。
有人可以请看看我是否做了任何输入错误,因为这是唯一不行的。
谢谢
你用Python 3而不是Python 2运行代码吗? – TerryA
我认为我的iPad的解释器可能正在运行Python 2.7 –
这是你的问题。此代码适用于python 3,但不适用于python 2.如果只能运行python 2.7,请将'input'更改为'raw_input' – TerryA