Why is the first if statement being triggered and not the elif statement after it?
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]))
John Kugelman
363k69 gold badges553 silver badges597 bronze badges
1 Answer 1
if 4 and 5 in computer_moves and 6 not in player_moves:
is same as
if 4 and (5 in computer_moves) and (6 not in player_moves):
,
change to
if 4 in computer_moves and 5 in computer_moves and 6 not in player_moves:
so
True and True and True
Same problem in the elif
.
elif 2 and 5 in computer_moves and 8 not in player_moves:
same as
elif 2 and (5 in computer_moves) and (8 not in player_moves):
answered Nov 14, 2021 at 7:16
-
If there's a lot of numbers to check for being in the list, or a different number each time, you can also use set subset operator:
{4, 5} <= {3, 4, 5, 6}
etc Commented Nov 14, 2021 at 7:33
lang-py