2017-07-15 97 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") 

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