0
clothes_total = tot1 + tot2 + tot3 + tot4+ tot5  
clothes_total1 = tot1 + tot2 + tot3+ tot4 + tot5
tot_price = tax * (clothes_total + shipping + gift_number)
tot_price1 = tax * (clothes_total1 * 0.85 + shipping + gift_number)
tot_price2 = tax * (clothes_total2 * 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
else clothes_total1 > 200:
  print "15% Discount + $30 off: $"
  print 0.85 * (clothes_total - 30)
  print "<h4> THIRTY: $ %s </h4>" % tot_price2

这是我的代码行,但在 下else clothes_total1 > 200:,我不断得到:

无效的语法错误

不知道是什么问题。有人可以帮我帮忙吗?谢谢你。

4

4 回答 4

3

elif它不应该是else

elif clothes_total1 > 200:

来自文档

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

而不是使用tot1 + tot2 + tot3 + tot4+ tot5,我认为你应该使用一个列表。

例子:

tot = [12,56,....] #some values

然后使用sum

clothes_total = sum(tot)

如果列表有超过 5 个项目并且您想要前 5 个项目的总和,则使用切片:

clothes_total = sum(tot[:5])
于 2013-07-28T05:18:13.403 回答
1

else不能有谓词。

使用elif而不是else.

elif clothes_total1 > 200:
于 2013-07-28T05:18:12.903 回答
0

else应该等于elif

这是因为在 Python 中,else它充当了一种“包罗万象”的角色——它不需要任何额外的要求,实际上也不允许它们。如果要为 else 子句指定条件,则必须使用elif.

于 2013-07-28T05:18:01.317 回答
0

else 不带条件,但 elif 带条件。如果您使用 else,这意味着,如果一切都失败了,请执行此操作。

于 2013-07-28T05:19:16.100 回答