簡體   English   中英

如何從此python代碼獲得正確的輸出?

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

我有一個 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))

誰能告訴我問題出在哪里? 非常感謝!

您應該將 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.")

並且,正如@locid 所評論的,將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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM