简体   繁体   English

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

[英]How to get the right output from this python code?

I have a python code, it cannot get the correct output.我有一个 python 代码,它无法获得正确的输出。 the code is shown as follows:代码如下所示:

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))

Can anyone tell me where is the problem?谁能告诉我问题出在哪里? Thank you so much!非常感谢!

You should change your if statement to:您应该将 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.")

And, as @loocid commented, change print("score is (0), level is (1)".format(score,grade)) to print("score is {0}, level is {1}".format(score,grade))并且,正如@locid 所评论的,将print("score is (0), level is (1)".format(score,grade))更改为print("score is {0}, level is {1}".format(score,grade))

Though I prefer虽然我更喜欢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM