2017-05-05 116 views
-2

我是新來編程,並開始學習Python.I做了一個簡單的計算器作爲練習和有問題與運行,因爲語法error.Could你告訴我,我犯的錯誤我的代碼:Python:我有一個好的語法嗎?

while True: 
    print("Options:") 
    print("Enter 'add' to add two numbers") 
    print("Enter 'substract' to substract two numbers") 
    print("Enter 'multiply' to multiply two numbers") 
    print("Enter 'divide' to divide two numbers") 
    print("Enter 'quit' to end the program") 
    user_input = input(":") 

    if user_input == "quit": 
     break 

    elif user_input == "add": 
     ... 
    elif user_input == "substract": 
     ... 
    elif user_input == "multiply": 
     ... 
    elif user_input == "divide": 
     ... 
    else: 
     print("Unknown input") 

    elif user_input == "add": 
     num1 = float(input("Enter a number:")) 
     num2 = float(input("Enter another number:")) 
     result = str(num1 + num2) 
     print("The answer is:" + result) 

    elif user_input == "substract": 
     num1 = float(input("Enter a number:")) 
     num2 = float(input("Enter another number:")) 
     result = str(num1 - num2) 
     print("The answer is:" + result) 

    elif user_input == "multiply": 
     num1 = float(input("Enter a number:")) 
     num2 = float(input("Enter another number:")) 
     result = str(num1 * num2) 
     print("The answer is:" + result) 

    elif user_input == "divide": 
     num1 = float(input("Enter a number;")) 
     num2 = float(input("Enter another number:")) 
     result = str(num1/num2) 
     print("The answer is:" + result) 

感謝您的反饋!

+2

Python是不夠好,爲您提供一個「例外」,這通常接近指向錯誤發生在哪裏。編輯你的問題並提供。 –

+0

它在部分顯示「無效的語法」錯誤:elif user_input ==「add」:在發佈我的問題之前,我嘗試了很多事情,但我跑出了想法,所以我在這裏尋求幫助。 –

+2

*編輯你的問題,並在那裏提供*。不作爲評論:-) –

回答

1

因爲你的第二個ELIF語句是在else語句之後。

而且我添加了一個tryexecpt塊的例子來處理ZeroDivisionError

while True: 
    print("Options:") 
    print("Enter 'add' to add two numbers") 
    print("Enter 'substract' to substract two numbers") 
    print("Enter 'multiply' to multiply two numbers") 
    print("Enter 'divide' to divide two numbers") 
    print("Enter 'quit' to end the program") 
    user_input = input(":") 

    if user_input == "quit": 
     break 
    elif user_input == "add": 
     num1 = float(input("Enter a number:")) 
     num2 = float(input("Enter another number:")) 
     result = str(num1 + num2) 
     print("The answer is:" + result) 

    elif user_input == "substract": 
     num1 = float(input("Enter a number:")) 
     num2 = float(input("Enter another number:")) 
     result = str(num1 - num2) 
     print("The answer is:" + result) 

    elif user_input == "multiply": 
     num1 = float(input("Enter a number:")) 
     num2 = float(input("Enter another number:")) 
     result = str(num1 * num2) 
     print("The answer is:" + result) 

    elif user_input == "divide": 
     num1 = float(input("Enter a number;")) 
     num2 = float(input("Enter another number:")) 
     # Example of a Try/Catch block to avoid ZeroDivisionError 
     try: 
      result = str(num1/num2) 
      print("The answer is:" + result) 
     except ZeroDivisionError: 
      print("Can't divide by 0") 
    else: 
     print("Unknown input") 
+0

非常感謝你,這最後的工作! –

+1

沒問題。如果這是有用的,你可以接受這個答案 – Ludisposed

相關問題