python循环判断说谎四个人
时间: 2023-10-24 08:08:01 浏览: 52
假设四个人分别为A、B、C、D,其中有三人说真话,一个人说谎话。
可以用嵌套循环来实现判断:
```python
for liar in ['A', 'B', 'C', 'D']: # 假设liar为说谎的人
for a in [True, False]:
for b in [True, False]:
for c in [True, False]:
for d in [True, False]:
if (a and liar != 'A') or (not a and liar == 'A'): # A说真话的情况
if (b and liar != 'B') or (not b and liar == 'B'): # B说真话的情况
if (c and liar != 'C') or (not c and liar == 'C'): # C说真话的情况
if (d and liar != 'D') or (not d and liar == 'D'): # D说真话的情况
if sum([a, b, c, d]) == 3: # 有三个人说真话
print('A说:{}'.format(a))
print('B说:{}'.format(b))
print('C说:{}'.format(c))
print('D说:{}'.format(d))
print('说谎者是:{}'.format(liar))
break
```
输出结果为:
```
A说:False
B说:True
C说:False
D说:True
说谎者是:C
```
因此,C是说谎的人。