我想通過在數組中添加1到數組中的項目,如果金錢== 100,並且如果沒有添加1到另一個項目,則嘗試在python中執行三元運算。但是我一直在得到一個無效的語法錯誤。添加整數的Python三元運算符
bills[2] += 1 if money == 100 else bills[1] += 1
^
SyntaxError: invalid syntax
這是代碼。
def tickets(people):
change =0
bills = [0,0,0]
for i,money in enumerate(people):
if money == 25:
change += 25
bills[0] += 1
str = "This is the %d th person with %d money" % (i,money)
print(str)
else:
bills[2] += 1 if money == 100 else bills[1] += 1
change -= (money -25)
str = "This is the %d th person with %d money" % (i,money)
print(str)
print("change is %d" % change)
if change < 0:
return "NO"
else:
return "YES"
是否要在其他部分將'bills [1] + 1'添加到'bills [2]'中? – letmutx