0

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
asked Nov 14, 2021 at 7:12

1 Answer 1

2

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
1
  • 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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.