score = int(input("What Score did you get?"))
if score <= 39 :
print ("You might want to try a bit harder, you got a grade U.")
if score =>40 and <=49:
print("Good, but you could try a little harder next time. You got a grade D")
if score =>50 and <= 59:
print ("Good Job! You got a grade C")
if score =>60 and <= 69:
print("Great! You got a grade B")
if score =>70:
print ("Excellent! Thats a grade A!")
答案 0 :(得分:3)
Python不是英文; and
不会猜测您正在测试同一个变量。你必须拼写出来:
if score >= 40 and score <= 49:
或者你可以使用比较链:
if 40 <= score <= 49:
其中Python基本上会做同样的事情(它与(40 <= score) and (score <= 49)
相同,但您不必重复score
变量或再使用and
。
但是,因为如果您使用if
.. elif
.. else
分支的组合,只有一个测试匹配,您可以测试而更高的值,只需要测试每个分支中的一个边界:
if score >= 70:
print("Excellent! Thats a grade A!")
elif score >= 60:
print("Great! You got a grade B")
elif score >= 50:
print("Good Job! You got a grade C")
elif score >= 40:
print("Good, but you could try a little harder next time. You got a grade D")
else:
print("You might want to try a bit harder, you got a grade U.")
请注意这与if
个单独系列语句之间的区别;这些不是彼此相关的,而使用elif
和else
非常重要。
因此,如果分数为70或以上,则第一次测试匹配并跳过所有测试。如果得分仅为55,那么前两个测试不匹配,但第三个测试不匹配,因此跳过余数。
如果您更喜欢订购,那么请测试上限而不是下限:
if score < 40:
print("You might want to try a bit harder, you got a grade U.")
elif score < 50:
print("Good, but you could try a little harder next time. You got a grade D")
elif score < 60:
print("Good Job! You got a grade C")
elif score < 70:
print("Great! You got a grade B")
else:
print("Excellent! Thats a grade A!")
答案 1 :(得分:2)
您缺少三个分数:
if score ==40 and score <=49:
而不是
if score ==40 and <=49:
最终守则:
score = int(input("What Score did you get?"))
if score <= 39 :
print ("You might want to try a bit harder, you got a grade U.")
if score ==40 and score<=49:
print("Good, but you could try a little harder next time. You got a grade D")
if score >=50 and score<= 59:
print ("Good Job! You got a grade C")
if score >=60 and score <= 69:
print("Great! You got a grade B")
if score >=70:
print ("Excellent! Thats a grade A!")
答案 2 :(得分:1)
您的语法错误发生在这里。
if score ==40 and <=49:
print("Good, but you could try a little harder next time. You got a grade D")
if score =>50 and <= 59:
print ("Good Job! You got a grade C")
if score =>60 and <= 69:
print("Great! You got a grade B")
Python的和语句结合了两个布尔语句,在您的情况下:
score =>60
<= 69
虽然第一个有效,但python无法识别第二个。我假设您正在比较两种情况下的得分变量,因此您可以通过在第二个布尔语句旁边添加得分来修复它,如下所示:
if score ==40 and score <=49:
print("Good, but you could try a little harder next time. You got a grade D")
if score =>50 and score <= 59:
print ("Good Job! You got a grade C")
if score =>60 and score <= 69:
print("Great! You got a grade B")