簡體   English   中英

Python無效語法錯誤(2.7.5)

[英]Python invalid syntax error (2.7.5)

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: ,我不斷得到:

無效的語法錯誤

不知道是什么問題。 有人可以幫我嗎? 謝謝。

它應該是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])

else不能有謂詞。

使用elif代替else

elif clothes_total1 > 200:

else應等於elif

這是因為在Python中, else充當了“包羅萬象”的角色-它不需要任何其他要求,實際上是不允許這樣做的。 如果要為else子句指定條件 ,則必須使用elif

否則沒有條件,但elif接受。 如果使用else,則意味着如果一切失敗,請執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM