简体   繁体   English

有if elif条件问题python2.7

[英]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."当我运行以下条件时,条件课程级别 6 超过 260 分,数学为 0,我没有得到“不幸的是,您不符合升入第三级的要求,至少 260 分并通过数学”是必要的。” message.信息。

But when run with the conditions course level 5 with over 260 points and 0 in maths I do get the message?但是,当在条件课程级别 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."

Once one test in an if-elif-else suite passes none of the other tests will be checked.一旦 if-elif-else 套件中的一个测试通过,其他任何测试都不会被检查。 In your code the condition in your elif is identical to the one in your if .在您的代码中,您的elif的条件与您的if相同。 That means the code under the elif will never be reached.这意味着永远无法访问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."

There is a better way to write your conditions.有一种更好的方法来编写您的条件。 Your error message contains the word and :您的错误消息包含单词and

At least 260 points and a pass in Mathematics are required.至少需要 260 分数学及格。

So use and in your condition to simplify your logic:所以在你的条件下使用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