Python 在if语句中执行错误的块 服装总计=总计1+总计2+总计3+总计4+总计5 总价=税金*(服装总额+运费+礼品编号) 总价格1=税*(衣服总额*0.85+运费+礼品编号) 总价格2=税*(衣服总数*0.85+运费+礼品编号-30) 打印“原价:$%s”%t总计 如果衣服总数150: 打印“15%折扣:$” 打印衣服总数*0.85 打印“十五:$%s”%tot_price 1 elif服装总数>200: 打印“15%折扣+30美元折扣:$” 打印0.85*(衣服总数-30件) 打印“三十:$%s”%tot_price 2

Python 在if语句中执行错误的块 服装总计=总计1+总计2+总计3+总计4+总计5 总价=税金*(服装总额+运费+礼品编号) 总价格1=税*(衣服总额*0.85+运费+礼品编号) 总价格2=税*(衣服总数*0.85+运费+礼品编号-30) 打印“原价:$%s”%t总计 如果衣服总数150: 打印“15%折扣:$” 打印衣服总数*0.85 打印“十五:$%s”%tot_price 1 elif服装总数>200: 打印“15%折扣+30美元折扣:$” 打印0.85*(衣服总数-30件) 打印“三十:$%s”%tot_price 2,python,python-2.7,if-statement,Python,Python 2.7,If Statement,即使costs\u total数字将大于200,elif costs\u total>200中的值也不会显示。你们能告诉我为什么它没出现吗?在elif costs\u total>150中,即使数字大于200,所有内容都显示得很好。我做错了什么?发生这种情况是因为您的程序在考虑elif costs\u total>200之前执行了elif costs\u total>150。下面是if语句的工作原理: 这: 与此相同: if condition1: do thing1 elif cond

即使
costs\u total
数字将大于200,
elif costs\u total>200
中的值也不会显示。你们能告诉我为什么它没出现吗?在
elif costs\u total>150
中,即使数字大于200,所有内容都显示得很好。我做错了什么?

发生这种情况是因为您的程序在考虑
elif costs\u total>200之前执行了
elif costs\u total>150
。下面是if语句的工作原理:

这:

与此相同:

if condition1:
    do thing1
elif condition2:
    do thing2
elif condition2:
    do thing3
如果要执行
If Cloth_total>150
If Cloth_total>200
中的内容,有四个选项:

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

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

选项4(范围条件):

如果衣服总数<150:
打印“总计:%s”%tot\u price
elif 150<衣服总数<200:#定义可接受值范围的界限
打印“15%折扣:$”
打印衣服总数*0.85
打印“十五:$%s”%tot_price 1
elif服装总数>200:
打印“15%折扣+30美元折扣:$”
打印0.85*(衣服总数-30件)
打印“三十:$%s”%tot_price 2
这使您能够短路所需的if语句,并保证在任何给定时间只输入一个块


希望这对您有所帮助

发生这种情况是因为您的程序在考虑到
elif costs\u total>200
之前执行了
elif costs\u total>150
。下面是if语句的工作原理:

这:

与此相同:

if condition1:
    do thing1
elif condition2:
    do thing2
elif condition2:
    do thing3
如果要执行
If Cloth_total>150
If Cloth_total>200
中的内容,有四个选项:

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

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

选项4(范围条件):

如果衣服总数<150:
打印“总计:%s”%tot\u price
elif 150<衣服总数<200:#定义可接受值范围的界限
打印“15%折扣:$”
打印衣服总数*0.85
打印“十五:$%s”%tot_price 1
elif服装总数>200:
打印“15%折扣+30美元折扣:$”
打印0.85*(衣服总数-30件)
打印“三十:$%s”%tot_price 2
这使您能够短路所需的if语句,并保证在任何给定时间只输入一个块


希望这有帮助

这是因为
如果elif else
条件短路,如果第一个
elif
条件为
True
则不会检查第二个条件

如果套件
,则从上开始:

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
它通过计算表达式1来选择其中一个套件 通过一个,直到发现一个是真的;然后执行该套件(以及 不执行或计算if语句的其他部分(参见)。如果有的话 如果表达式为false,则else子句的套件(如果存在)为false 执行

如果要执行所有条件,请使用所有
(如果
):

if_stmt ::=  "if" expression ":" suite
             ( "elif" expression ":" suite )*
             ["else" ":" suite]
如果衣服总数<150:
...
如果衣服总数>150:
...
如果衣服总数>200:
...
另一个选择是:

if clothes_total < 150:
    ...
if clothes_total > 150:
    ...
if clothes_total > 200:
  ...
如果衣服总数<150:
...

elif 150这是因为
如果elif else
条件短路,如果第一个
elif
条件为
True
则不检查第二个条件

如果套件
,则从上开始:

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
它通过计算表达式1来选择其中一个套件 通过一个,直到发现一个是真的;然后执行该套件(以及 不执行或计算if语句的其他部分(参见)。如果有的话 如果表达式为false,则else子句的套件(如果存在)为false 执行

如果要执行所有条件,请使用所有
(如果
):

if_stmt ::=  "if" expression ":" suite
             ( "elif" expression ":" suite )*
             ["else" ":" suite]
如果衣服总数<150:
...
如果衣服总数>150:
...
如果衣服总数>200:
...
另一个选择是:

if clothes_total < 150:
    ...
if clothes_total > 150:
    ...
if clothes_total > 200:
  ...
如果衣服总数<150:
...

elif 150您是否知道您可以对列表进行求和:

total=sum([tot1,tot2,tot3,tot4,tot5])

但我必须相信你有一个总变量的列表,在这种情况下你可以做:


Clothis\u total=sum(totals\u list)

您知道您可以对列表进行汇总吗:

total=sum([tot1,tot2,tot3,tot4,tot5])

但我必须相信你有一个总变量的列表,在这种情况下你可以做:


total=sum(totals\u list)

正如其他人所指出的,一旦
total>150
条件为真,则
total>200
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
  ...
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