【发布时间】:2017-05-23 18:51:11
【问题描述】:
我正在尝试在 python 中进行三元运算,如果 money == 100 则将 1 添加到数组中的项目,如果没有,则将 1 添加到另一个项目。但我不断收到无效的语法错误。
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"
【问题讨论】:
-
你想在else部分将
bills[1] + 1添加到bills[2]吗?
标签: python python-3.x conditional-operator