首页 > 解决方案 > 坚持使用 Python if-else 一行语句

问题描述

在进行赋值操作时,我遇到了嵌套单行条件语句的问题。

bob   = list(map(int, input().split()))
print(alice, bob)
a, b = 0
for i in range(3):
    a+=1 if alice[i]>bob[i] else (b+=1 if alice[i]<bob[i] else 1)
print(a,b)```

Output: <br/>
File "/home/Algorithms/compare_triplets.py", line 6    
a+=1 if alice[i]>bob[i] else b=b+1 if alice[i]<bob[i] else 1

标签: pythondata-structures

解决方案


您提供的代码有点不清楚,也没有解释明确的目标。这是基于我的假设实现接近目标的代码:

  bob   = list(map(int, input().split()))
 print(alice, bob)
 a, b = 0
for i in range(3):

if alice[i]>bob[i]:
    a+=1 
else:
    b+=1
 print(a,b)

推荐阅读