2017-07-15 94 views
0

我试图在蟒蛇一个简单的计算器,但我似乎得到的输入错误,每当我尝试运行它:输入错误

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每当我输入内容时(用输入替换省略号)。

有人可以请看看我是否做了任何输入错误,因为这是唯一不行的。

谢谢

+0

你用Python 3而不是Python 2运行代码吗? – TerryA

+0

我认为我的iPad的解释器可能正在运行Python 2.7 –

+0

这是你的问题。此代码适用于python 3,但不适用于python 2.如果只能运行python 2.7,请将'input'更改为'raw_input' – TerryA

回答

0

你好user8311329,

问题

当您使用字符串作为输入,因此使用raw_input()功能。并且你使用的所有程序都是;,所以python提供了用;(colon)来执行程序的功能。

解决方案

试试这个代码,

while True: 
    print("please choose one of these available options: \n1. Add \n2. Subtract \n3. Multiply \n4. Divide \n5. Quit \nType in exactly as shown or the calculator won't work") 
    choice = str(raw_input("From above choice any one option: ")) 

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

我给的建议,例如,如果任何其他用户运行这个使用你的应用程序,这样这个人有什么不明白的输入准确输入所以代码编写高效和完善,所以其他用户容易理解,并给出建议不使用此代码的字符串插入,但使用数字输入的选择像我举例(我不给只是简单的例子,你也添加更多的验证输入),

while True: 
    print("please choose one of these available options: \n1. Add \n2. Subtract \n3. Multiply \n4. Divide \n5. Quit \nType in exactly as shown or the calculator won't work") 
    try: 
     choice = input("From above choice any one option: ") 

     if(choice == 1): 
      num1 = float(input("Please enter a number:")) 
      num2 = float(input("Please enter another number:")) 
      output = num1+num2 
      print "The answer is:",output,"\n" 

     elif(choice == 2): 
      num1 = float(input("Please enter a number:")) 
      num2 = float(input("Please enter another number:")) 
      output = num1-num2 
      print "The answer is:",output,"\n" 

     elif(choice == 3): 
      num1 = float(input("Please enter a number:")) 
      num2 = float(input("Please enter another number:")) 
      output = num1*num2 
      print "The answer is:",output,"\n" 

     elif(choice == 4): 
      num1 = float(input("Please enter a number:")) 
      num2 = float(input("Please enter another number:")) 
      output = num1/num2 
      print "The answer is:",output,"\n" 


     elif(choice == 5): 
      break 

     else: 
      print "Invalid Input. Try again\n" 
    except: 
     print "Invalid Input. Try again...\n" 

我希望我的回答有帮助。
如果有任何查询请这么评论。

+1

谢谢。提示工作 –

+0

我做了upvote答案 –

+0

好吧现在显示非常感谢你... –

0

只是改变输入(),到raw_input()。与输入()相比,raw_input()将输入作为字符串输入,而输入()会决定你的输入。

也,你可以把打印里面输入:

choice = raw_input("please choose one of these available options:\n\"add, subtract, multiply, divide, quit\"\ntype in exactly as shown or the calculator won't work") 

也,你并不需要 ';'在每一行的蟒蛇到底