triple02.md 981 字节
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
# Python 三元表达式(2)

使用嵌套的三元组表达式统计数字频率,如果是2的倍数加1,如果是4的倍数加2,否则加0

```python

if __name__ == '__main__':
    pi = [3, 14, 15, 9, 26, 5, 35, 8, 97, 932]
    even_count = 0

    for i in pi:
        # TODO(You): 请在此实现统计代码
    assert even_count == 6
```

请选出下列能**正确**实现这一功能的选项。

## template

```python

if __name__ == '__main__':
    pi = [3, 14, 15, 9, 26, 5, 35, 8, 97, 932]
    even_count = 0

    for i in pi:
        even_count += 2 if i % 4 == 0 else 1 if i % 2 == 0 else 0
    assert even_count == 6
```

## 答案

```python
even_count += 2 if i % 4 == 0 else 1 if i % 2 == 0 else 0
```

## 选项

### A

```python
even_count += if i % 4 == 0: 2 else: if i % 2 == 0: 1 else: 0
```

### B

```python
even_count += 1 if i % 2 == 0 else 2 if i % 4 == 0 else 0
```

### C

```python
even_count += 2 if i % 2 == 0 else 1 if i % 4 == 0 else 0
```