简体   繁体   中英

Having if elif condition problems python2.7

When I run the following, with the conditions course level 6 with over 260 points and a 0 in maths I'm not getting the "Unfortunately you do not meet the requirements to progress to third level, At least 260 points and a pass in Mathematics are required." message.

But when run with the conditions course level 5 with over 260 points and 0 in maths I do get the message?

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."

Once one test in an if-elif-else suite passes none of the other tests will be checked. In your code the condition in your elif is identical to the one in your if . That means the code under the elif will never be reached.

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."

There is a better way to write your conditions. Your error message contains the word and :

At least 260 points and a pass in Mathematics are required.

So use and in your condition to simplify your logic:

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."

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