Comparing guesses with backpack items

In the math chapter, we learned about something called modulo. Now, it is coming back. In the backpack game, we compare the items in one player's backpack to the guess of another player. Then, the players switch places! How will the computer keep track of which backpack to look at and what player should be chosen? We can use modulo to help us always choose the correct player in the two-player version of this game.

Here is the line of code that uses modulo (line 27):

other_player = players[(i+1) % 2]

This line of code uses modulo to identify the opposite player by looking for the player in the players list we made in line 5. Here is the basic idea:

  • Erin (player 1) = 0 and Tanvir (player 2) = 1.
  • Whoever is playing ...

Get Python Projects for Kids now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.