在ifstatemen中执行错误的块

2024-04-20 10:14:35 发布

您现在位置:Python中文网/ 问答频道 /正文

clothes_total = tot1 + tot2 + tot3 + tot4+ tot5
tot_price = tax * (clothes_total + shipping + gift_number)
tot_price1 = tax * (clothes_total * 0.85 + shipping + gift_number)
tot_price2 = tax * (clothes_total * 0.85 + shipping + gift_number - 30)
print "<h4>Original Price: $ %s </h4>" % clothes_total
if clothes_total < 150:
    print "<h4> TOTAL : %s </h4>" % tot_price
elif clothes_total > 150:
    print "15% Discount: $"
    print clothes_total * 0.85
    print "<h4> FIFTEEN: $ %s </h4>" % tot_price1
elif clothes_total > 200:
    print "15% Discount + $30 off: $"
    print 0.85 * (clothes_total - 30)
    print "<h4> THIRTY: $ %s </h4>" % tot_price2

即使clothes_total数字大于200,elif clothes_total >200中的值也不会出现。你们能告诉我为什么它没有出现吗?在elif clothes_total > 150中,即使数字大于200,一切都很好地显示出来。我做错什么了?你知道吗


Tags: numberdiscount数字priceh4totalshippingtax
3条回答

这是因为if-elif-else条件短路,如果第一个elif条件是True,那么第二个条件就不会被检查。你知道吗

docsif-suite

if_stmt ::=  "if" expression ":" suite
             ( "elif" expression ":" suite )*
             ["else" ":" suite]

It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true; then that suite is executed (and no other part of the if statement is executed or evaluated). If all expressions are false, the suite of the else clause, if present, is executed.

如果希望执行所有条件,请使用所有if

if clothes_total < 150:
    ...
if clothes_total > 150:
    ...
if clothes_total > 200:
  ...

另一种选择是:

if clothes_total < 150:
    ...
elif 150 <= clothes_total <= 200:  #this is True if clothes_total is between 150 and 200(both inclusive)
    ...
elif clothes_total > 200:          #True if clothes_total is greater than 200
  ...

这是因为您的程序执行在考虑elif clothes_total > 200之前经过了elif clothes_total > 150。下面是if语句的工作原理:

这是:

if condition1:
    do thing1
elif condition2:
    do thing2
elif condition2:
    do thing3

与此相同:

if condition1:
    do thing1
else:
    if condition2:
        do thing2
    else:
        if condition2:
            do thing3

如果要执行if clothes_total > 150if clothes_total > 200中的内容,有四个选项:

选项1(只需将所有内容从一个添加到另一个):

if clothes_total < 150:
    print "<h4> TOTAL : %s </h4>" % tot_price
elif 150 < clothes_total < 200: # define a maximum as well
    print "15% Discount: $"
    print clothes_total * 0.85
    print "<h4> FIFTEEN: $ %s </h4>" % tot_price1
    print "15% Discount + $30 off: $"
    print 0.85 * (clothes_total - 30)
    print "<h4> THIRTY: $ %s </h4>" % tot_price2
elif clothes_total > 200:
    print "15% Discount + $30 off: $"
    print 0.85 * (clothes_total - 30)
    print "<h4> THIRTY: $ %s </h4>" % tot_price2

选项2(嵌套if语句):

if clothes_total < 150:
    print "<h4> TOTAL : %s </h4>" % tot_price
elif 150 < clothes_total:
    print "15% Discount: $"
    print clothes_total * 0.85
    print "<h4> FIFTEEN: $ %s </h4>" % tot_price1
    if clothes_total > 200:
        print "15% Discount + $30 off: $"
        print 0.85 * (clothes_total - 30)
        print "<h4> THIRTY: $ %s </h4>" % tot_price2
elif clothes_total > 200:
    print "15% Discount + $30 off: $"
    print 0.85 * (clothes_total - 30)
    print "<h4> THIRTY: $ %s </h4>" % tot_price2

选项3(没有else,只有if):

if clothes_total < 150:
    print "<h4> TOTAL : %s </h4>" % tot_price
if 150 < clothes_total
    print "15% Discount: $"
    print clothes_total * 0.85
    print "<h4> FIFTEEN: $ %s </h4>" % tot_price1
if clothes_total > 200:
    print "15% Discount + $30 off: $"
    print 0.85 * (clothes_total - 30)
    print "<h4> THIRTY: $ %s </h4>" % tot_price2

这将执行最后两个if块,这可能不是您想要的。但是,请注意,在执行所有这些if语句的条件时,您会在运行时失败,特别是当它们是复杂的条件时

选项4(范围条件):

if clothes_total < 150:
    print "<h4> TOTAL : %s </h4>" % tot_price
elif 150 < clothes_total < 200: # define the bounds of the range of acceptable values
    print "15% Discount: $"
    print clothes_total * 0.85
    print "<h4> FIFTEEN: $ %s </h4>" % tot_price1
elif clothes_total > 200:
    print "15% Discount + $30 off: $"
    print 0.85 * (clothes_total - 30)
    print "<h4> THIRTY: $ %s </h4>" % tot_price2

这使您可以对所需的if语句进行短路,并保证在任何给定时间只输入一个块。你知道吗

希望这有帮助

正如其他人指出的,一旦total>150条件为真,total>200甚至不会被计算。你知道吗

试着把这些陈述的顺序颠倒过来。。。你知道吗

if clothes_total > 200:
    print "15% Discount + $30 off: $"
    print 0.85 * (clothes_total - 30)
    print "<h4> THIRTY: $ %s </h4>" % tot_price2
elif clothes_total > 150:
    print "15% Discount: $"
    print clothes_total * 0.85
    print "<h4> FIFTEEN: $ %s </h4>" % tot_price1
else:
    print "<h4> TOTAL : %s </h4>" % tot_price

还请记住,在原始代码中,如果不将<更改为<=,那么值正好为150的代码就会丢失。你知道吗

相关问题 更多 >