2017-05-03 111 views
-2

這裏是我的代碼:基本if語句不工作 - Python的

input_score = raw_input("Enter Score: ") 
score = float(input_score) 

if score >= 0.9 and <= 1.0: 
    print "A" 

elif score >= 0.8 and < 0.9: 
    print "B" 

elif score >= 0.7 and < 0.8: 
    print "C" 

elif score >= 0.6 and < 0.7: 
    print "D" 

elif score < 0.6 and >= 0.0: 
    print "F" 

else : 
print "Error" 

我一直在第4行得到一個消息,說我有一個語法錯誤。我不確定我在做什麼錯誤

+1

'如果分數> = 0.9和<= 1.0:' =>'如果分數> = 0.9,得分<= 1.0:' –

回答

1

您必須在if score >= 0.9 and score <= 1.0(例如)中指定score。這被解析爲if (score >= 0.9) and (score <= 1.0) - 寫if (score >= 0.9) and (<= 1.0)沒有意義,因爲第二部分是一個單獨的表達式。

+1

太謝謝你了...工作! – Stephan

2

您不能在python if score >= 0.9 and <= 1.0:中編寫代碼,因爲您的表達式中得分不與1.0相比,而只有0.9。你可以寫if score >= 0.9 and score <= 1.0:。 Python的實際上允許你把它寫在較短的格式如下:

if 1.0>= score >= 0.9: 
+1

我感謝你的幫助..這工作! – Stephan