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,一切都很好地显示出来。我做错什么了?你知道吗
这是因为
if-elif-else
条件短路,如果第一个elif
条件是True
,那么第二个条件就不会被检查。你知道吗从docs到
if-suite
:如果希望执行所有条件,请使用所有
if
:另一种选择是:
这是因为您的程序执行在考虑
elif clothes_total > 200
之前经过了elif clothes_total > 150
。下面是if语句的工作原理:这是:
与此相同:
如果要执行
if clothes_total > 150
和if clothes_total > 200
中的内容,有四个选项:选项1(只需将所有内容从一个添加到另一个):
选项2(嵌套if语句):
选项3(没有
else
,只有if
):这将执行最后两个
if
块,这可能不是您想要的。但是,请注意,在执行所有这些if语句的条件时,您会在运行时失败,特别是当它们是复杂的条件时选项4(范围条件):
这使您可以对所需的if语句进行短路,并保证在任何给定时间只输入一个块。你知道吗
希望这有帮助
正如其他人指出的,一旦
total>150
条件为真,total>200
甚至不会被计算。你知道吗试着把这些陈述的顺序颠倒过来。。。你知道吗
还请记住,在原始代码中,如果不将
<
更改为<=
,那么值正好为150的代码就会丢失。你知道吗相关问题 更多 >
编程相关推荐