Mopps
Mopps

Reputation: 13

Why is this if statement being triggered when the list does not contain the conditions the if statement specifies?

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]))

Upvotes: 0

Views: 34

Answers (1)

buran
buran

Reputation: 14233

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):

Upvotes: 2

Related Questions