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 needs to compare their answer to the backpack of the other player.
To get the backpack of the other player, we tell the computer Hey, we need the backpack of the player who is NOT guessing right now. We do this with math.
Erin needs to use Tanvir...