简体   繁体   中英

How to get the right output from this python code?

I have a python code, it cannot get the correct output. 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 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))

Though I prefer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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