python - 当列表不包含 if 语句指定的条件时,为什么会触发这个 if 语句?
问题描述
为什么触发第一个 if 语句而不是它之后的 elif 语句?
player_moves = [1, 3, 4]
computer_moves = [5, 2]
if 4 and 5 in computer_moves and 6 not in player_moves:
computer_moves.append(6)
print("Computer chooses " + str(computer_moves[-1]))
elif 2 and 5 in computer_moves and 8 not in player_moves:
computer_moves.append(8)
print("Computer chooses " + str(computer_moves[-1]))
解决方案
if 4 and 5 in computer_moves and 6 not in player_moves:
和
if 4 and (5 in computer_moves) and (6 not in player_moves):
,
改成
if 4 in computer_moves and 5 in computer_moves and 6 not in player_moves:
所以
True and True and True
中的同样问题elif
。
elif 2 and 5 in computer_moves and 8 not in player_moves:
如同
elif 2 and (5 in computer_moves) and (8 not in player_moves):
推荐阅读
- c# - 如何杀死网络上的用户进程?使用路径
- java - JCheckBoxTree 初始状态问题
- r - 疏浚函数顶级模型 (lmer) 变量的置信区间
- python - 读取文件时如何删除一行的第一部分?
- entity-framework-core - EF Core 中的获取迁移
- django - djoser 通过链接激活帐户
- ios - 在 Flutter iOS 中找不到 FirebaseInstanceId/FirebaseInstanceId.h
- excel - 性能 Sumproduct 与 Application.Worksheetfunction.Sumproduct
- laravel - 在 Laravel 进程上无头运行 chrome
- django - 'QuerySet' 对象没有属性 'images'