I have the following code:
choice = ['rock', 'scissors', 'paper'] # Game loop while True: player1 = input("Player 1 - Rock, Paper, Scissors? ").lower() player2 = input("Player 2 - Rock, Paper, Scissors? ").lower() if choice.index(player1) == (choice.index(player2) + 1) % 3: print('Player 2 wins!') break elif choice.index(player2) == (choice.index(player1) + 1) % 3: print('Player 1 wins!') break else: print("Its a draw! Try again")
I’ve been trying to figure out how it works, but can’t get my head around +1 % 3 part. Can someone explain?
If you change the choice dictionary to
choice = ['rock', 'paper', 'scissors']
The code breaks, it doesn’t seem to work unless the dictionary is rock, scissors, paper.
Thanks in advance.