2016-05-31 94 views
-3

這裏是我的代碼的Python 2.7未知的語法錯誤

hrs = raw_input("Enter Hours:") 
h = float(hrs) 
rate = raw_input("Enter Rate:") 
r = float(rate) 

if hrs <= 40 
    pay = hrs * rate 
    print pay 

else hrs > 40 
    pay = hrs * 15.75 
    print pay 

以下是錯誤消息

486406789.415.py", line 6 
    if hrs <= 40 
      ^
SyntaxError: invalid syntax 
+1

[if語句在條件後需要冒號(:))(https://docs.python.org/3/tutorial/controlflow.html# if語句) –

回答

0

有在你的代碼的多個錯誤。無效的語法錯誤很容易解決,只需在if語句的末尾添加冒號,其他語句不會採取任何條件(但是,elif,但會)。這是非常基本的Python語法。您可以隨時查看官方的Python教程和文檔,作爲解決語法錯誤的第一步。例如: -

http://www.tutorialspoint.com/python/python_if_else.htm

您解決與整數比較字符串時,在hrs <= 40你會遇到的其他問題的SyntaxError後。相反,你想比較你的轉換輸入h。計算也是如此:使用h而不是hrsr而不是速率。

看看你是否可以解決所有問題。如果你不管理,這裏是一個代碼的工作示例:https://gist.github.com/fabianegli/bae9864e5166fac4dd2baeccd5ed3f8d

+0

謝謝你,它幫了我很多! – Kyler

+0

不客氣! – fabianegli

1

你缺少的條件後冒號(:)。另外請注意,else並不需要一個條件,你需要使用elif

if hrs <= 40: 
    # Here -^ 
    pay = hrs * rate 
    print pay 

elif hrs > 40: # Note the elif 
    # Here --^ 
    pay = hrs * 15.75 
    print pay 
+0

非常感謝,非常有幫助! – Kyler