簡體   English   中英

有if elif條件問題python2.7

[英]Having if elif condition problems python2.7

當我運行以下條件時,條件課程級別 6 超過 260 分,數學為 0,我沒有得到“不幸的是,您不符合升入第三級的要求,至少 260 分並通過數學”是必要的。” 信息。

但是,當在條件課程級別 5 下運行時,我確實得到了超過 260 分和 0 分的數學信息?

if course == 5:
    print"Your total CAO points are " ,total

elif course == 6:
        print"Your total CAO points are " , total*1.25
        total= total*1.25

if total >=260: 
    if math>=25:
        print "You are eligible to progress to third level"

elif total >=260:
     math=0
        print "Unfortunately you do not meet the requirements to progress to third level, At least 260 points and a pass in Mathematics are required."

elif total <=260:
    math=0
    print "Unfortunately you do not meet the requirements to progress to third level, At least 260 points and a pass in Mathematics are required."

一旦 if-elif-else 套件中的一個測試通過,其他任何測試都不會被檢查。 在您的代碼中,您的elif的條件與您的if相同。 這意味着永遠無法訪問elif下的代碼。

if total >= 260: 
    if math >= 25: # failing this if-statement does not negate the "if total >= 260"
        print "You are eligible to progress to third level"

elif total >= 260:  # will never be reached because it's identical to prior if-statement
    math=0
    print "Unfortunately you do not meet the requirements to progress to third level, At least 260 points and a pass in Mathematics are required."

elif total <= 260: # is 260 in our out? if 260 is a pass change from <= to <
    math=0
    print "Unfortunately you do not meet the requirements to progress to third level, At least 260 points and a pass in Mathematics are required."

有一種更好的方法來編寫您的條件。 您的錯誤消息包含單詞and

至少需要 260 分數學及格。

所以在你的條件下使用and來簡化你的邏輯:

if total >= 260 and math >= 25:     # must meet both conditions
    print "You are eligible to progress to third level"
else:
    math = 0
    print "Unfortunately you do not meet the requirements to progress to third level. "\
        "At least 260 points and a pass in Mathematics are required."

暫無
暫無

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

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