如何从此python代码获取正确的输出?

时间:2019-01-14 01:35:50

标签: python

我有一个python代码,它无法获得正确的输出。代码如下所示:

score = int(input("Please input a score: "))
grade = ""
if score < 60:
    grade = "failed"
elif score < 80:     # between 60 and 80
    grade = "pass"
elif score < 90:
    grade = "good"
else:
    grade = "excellent"

print("score is (0), level is (1)".format(score,grade))

谁能告诉我问题出在哪里?非常感谢!

1 个答案:

答案 0 :(得分:0)

您应该将if语句更改为:

if score <= 60:
    grade = "failed"
elif score <= 80 and score > 60:     # between 60 and 80
    grade = "pass"
elif score <= 90 and score > 80:
    grade = "good"
elif score > 90: #Assuming there is no max score since before, you left the last statement as an else. 
    grade = "excellent"
else: #Not necessary but always nice to include.
    print("Not a valid score, please try again.")

然后,如@loocid所说,将print("score is (0), level is (1)".format(score,grade))更改为print("score is {0}, level is {1}".format(score,grade))

尽管我更喜欢

print("score is " + str(score) + ", level is " + str(grade))