2017-05-03 132 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