[英]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.