我目前正在嘗試編寫一個小計算器,因爲我正在學習python。我的問題是,它始終在else語句處輸出Syntax錯誤,因爲我仍然是初學者,我不知道爲什麼。 :( Code of my calculator其他語句語法錯誤(簡單計算器)
-2
A
回答
0
你缺少一個colon :
與其他
應該else: {your logic}
更新:其實你有結腸,但有一個條件,即應該是一個。 elif
而不是一個else
更改最後else
到elif
,如果沒有默認檢查,你不一定需要別的東西。
0
實際上你的代碼有幾個問題。
你是不是字符串和整數等
"4" + "5"
之間的轉換是不是9
,這是"45"
,因爲它結合了兩個字符串。但如果你這樣做int("4") + int("5")
那麼你會得到9.當做一個else語句時,沒有條件。
因此,一個基本如果elif的,否則將是:
a = "yay"
if a == "yay":
print("a likes you")
elif a == "no":
print("a doesn't like you")
else:
print("a doesn't want to respond")
的Python 2.7
print ("Welcome to your friendly Python calculator. Use + for addition and - for substraction")
print ("This code uses period (.) for decmimals")
first = "Please enter your first number "
second = "Please enter your second number "
operator = raw_input("Please choose an operation (+ or -) ")
if operator == "+":
num1 = input(first)
num2 = input(second)
print ("Result: " + str(num1 + num2))
elif operator == "-":
num1 = input(first)
num2 = input(second)
print ("Result: " + str(num1 - num2))
else:
print("You didn't enter a valid operator.")
的Python 3.6
print ("Welcome to your friendly Python calculator. Use + for addition and - for substraction")
print ("This code uses period (.) for decmimals")
first = "Please enter your first number "
second = "Please enter your second number "
operator = input("Please choose an operation (+ or -) ")
if operator == "+":
num1 = int(input(first))
num2 = int(input(second))
print ("Result: " + str(num1 + num2))
elif operator == "-":
num1 = int(input(first))
num2 = int(input(second))
print ("Result: " + str(num1 - num2))
else:
print("You didn't enter a valid operator.")
相關問題
- 1. 簡單的PHP命中計數器/如果其他語句
- 2. 簡單Where子句有語法錯誤
- 3. javascript,簡單如果其他語句
- 4. 其他Python語法錯誤
- 5. 其他語法錯誤Python
- 6. Switch中的其他語句是語法錯誤?
- 7. 簡單語法錯誤
- 8. 簡單的SQL查詢語句的語法錯誤
- 9. 兩個簡單的MySQL語句導致語法錯誤
- 10. 簡單如果else語句中的語法錯誤在R
- 11. 如果其他語句錯誤
- 12. 在SELECT語句中計算錯誤
- 13. 第一天程序員其他語句爲計算器程序
- 14. Sqlite語句語法錯誤
- 15. SQL語句語法錯誤
- 16. 帶計算列的語法case語句
- 17. Java - 使用If和if else語句的簡單稅計算器
- 18. 簡單的JavaScript計算器如果語句
- 19. 語法錯誤插入語句錯誤
- 20. 語法錯誤FROM子句(from子句最簡單的可能)
- 21. 簡單SQL更新語句語法
- 22. 其他:語法錯誤的Python
- 23. 「如果」「其他」與語法錯誤
- 24. 語法錯誤,意外的其他T_ELSE
- 25. awk語法和非法語句錯誤
- 26. 當Xtext語法擴展其他Xtext語法時出現錯誤
- 27. 其他語句的單獨return語句ANTRL
- 28. jQuery語法錯誤? CSS和動畫的 '如果' 和 '其他' 語句
- 29. 語法簡單PHP的錯誤
- 30. 簡單的SQL語法錯誤
這裏您需要另一個'elif'聲明。在'else'之後,不能檢查其他條件(因此'else')。 – Jan
我把else改成了elif,但它仍然輸出無效語法,並標記elif紅色。 – Skulptis
請將您的代碼添加爲文字,而不是圖片。 – user2314737