Book Image

Python Projects for Kids

By : Jessica Ingrassellino
Book Image

Python Projects for Kids

By: Jessica Ingrassellino

Overview of this book

Kids are always the most fast-paced and enthusiastic learners, and are naturally willing to build stuff that looks like magic at the end (when it works!). Programming can be one such magic. Being able to write a program that works helps them feel they've really achieved something. Kids today are very tech-savvy and cannot wait to enter the fast-paced digital world. Because Python is one of the most popular languages and has a syntax that is quite simple to understand, even kids are eager to use it as a stepping stone to learning programming languages. This book will cover projects that are simple and fun, and teach kids how to write Python code that works. The book will teach the basics of Python programming, installation, and so on and then will move on to projects. A total of three projects, with each and every step explained carefully, without any assumption of previous experience.
Table of Contents (18 chapters)
Python Projects for Kids
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

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...