小编Edo*_*gbu的帖子

Python三元运算符添加整数

我试图在python中进行三元运算,如果money == 100,则在数组中的项目中添加1,如果不是,则在另一个项目中添加1.但我继续得到无效的语法错误.

 bills[2] += 1 if money == 100 else bills[1] += 1
                                             ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

这是代码.

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 …
Run Code Online (Sandbox Code Playgroud)

python conditional-operator python-3.x

0
推荐指数
1
解决办法
262
查看次数

标签 统计

conditional-operator ×1

python ×1

python-3.x ×1