[Solved] Am not understanding how the index() line work [closed]


choice.index(p1) == (choice.index(p2) + 1) is true when the position of p2 in the list is one more than the position of p1 in the list, i.e. p2 is right after p1 in the list.

choice.index(p1) == (choice.index(p2) + 1) % 3 is the same thing, but it considers the start of the list to come right after the end of the list. The end of the list is index 2 (choice[2] == 'scissors'), 2 + 1 is 3, and 3 % 3 is 0 – the start of the list.

The list is arranged so that every choice comes after what it loses to in that way. paper (0), rock (1) – paper beats rock; rock (1), scissors (2) – rock beats scissors; scissors (2), paper (0) – scissors beats paper.

Modular arithmetic on Wikipedia

solved Am not understanding how the index() line work [closed]