Captain Code: Unleash Your Coding Superpower with Python, 1st edition

Published by Addison-Wesley Professional (December 21, 2021) © 2022

  • Ben Forta
  • Shmuel Forta

VitalSource eTextbook

ISBN-13: 9780137653614
Captain Code: Unleash Your Coding Superpower with Python
Published 2021
  • Available for purchase from all major ebook resellers, including InformIT.com

Paperback

ISBN-13: 9780137653577
Captain Code: Unleash Your Coding Superpower with Python
Published 2021

Details

  • A print text
  • Free shipping
  • Also available for purchase as an ebook from all major ebook resellers, including InformIT.com

"This is the book I needed that I did not know I needed. Having taught myself Python and having taught students, there are still some gaps in my knowledge that Captain Code quickly filled. The overall design of the book is wonderful and easy to read. All of the questions I have normally had when it comes to Python are answered on the pages. The code is cleanly written and the ability to scan a QR code so you can copy the code digitally is a major time-saver. What is most evident about this book is that it has been written by people that have worked with students. Too often books are written for students by people who have never taught and it never translates well on the pages. Captain Code is accessible for students and teachers and I think it is a must have for any classroom that is exploring Python. I know I will keep one at home for me and a few more in the classroom."

Nicholas Provenzano

TheNerdyTeacher

@TheNerdyTeacher

Author - Your Starter Guide to Makerspaces, The Maker Mentality, and Beyond the Poster Board

MACUL OutstandingTechnology Using Teacher of the Year

ISTE Outstanding Teacher of the Year

Captain Code is a welcoming, engaging, and fun introduction to becoming a coder, designed for young students (ages 10-17) and the young-at-heart. Experienced educators and coders Ben & Shmuel Forta guide students using Python, one of the most popular programming languages in the world. Students will learn by creating games—from simple projects, to retro text-based adventures, to complete graphical arcade style games. Captain Code is 400 glossy color pages of goodness packed with welcoming images, useful tips and tidbits, and engaging, readable text that focuses on doing while having fun. All code listings are in full-color and QR codes link to bonus content, downloads, challenge solutions, and more.

Captain Code makes coding exciting and rewarding, as it prepares a new generation to take their next steps forward—in education, careers, or both.

Introduction     xv

PART I:  IT'S ALL FUN AND GAMES     1

Chapter 1  Getting Started     3

Understanding Computer Programming     4

    What is a computer?     4

    How do we talk to computers?     5

    What is Python?     8

Setting Things Up     9

    Installing Python     9

    Installing and Configuring Visual Studio Code     10

    Creating a Work Folder     13

Writing Your First Python Program     15

    Selecting Your Work Folder     16

    It's Coding Time!     17

Summary     19

Chapter 2  Mad Libs     21

Understanding Functions     22

Using Variables     23

    Creating a Variable     24

    Using a Variable     24

    Some Important Variable Rules     25

    Variables, More Variables, and Even More Variables     26

    Getting User Input     28

Playing Mad Libs     30

    Write Your Story     30

    Add Variables     30

    Get User Input     32

Summary     33

Chapter 3  Roll the Dice     35

Using Libraries     36

    The random Library     36

    Generating Random Numbers     37

    Choosing a Random Item     38

“3” Is Not 3     41

Commenting Your Code     43

One Die, Two Dice     45

Summary     49

Chapter 4  Calculate the Day     51

Working with Dates     52

    The datetime Library     52

    Using the datetime Class     55

Making Decisions     56

    The if Statement     56

    What else?     58

    if Revisited     59

    Testing for Other Options     61

    Using in     62

Beating the Mathematician     63

    Handling Numeric Inputs     63

    Putting It All Together     64

    An Alternate Solution     67

Summary     67

Chapter 5  Rock Paper Scissors     69

More Strings     70

Game Time     72

    Handling User Input     72

    The Game Code     74

    One Last Tweak     76

Summary     77

Chapter 6  Secret Codes     79

Lists     80

    Creating Lists     80

    Accessing List Items     82

    Changing List Items     83

    Adding and Removing Items     84

    Finding Items     85

    Sorting     86

Loop-de-Loop     89

    Looping Through Items     90

    Looping Through Numbers     92

    Nested Loops     93

Cracking the Code     95

    Encrypting Characters     96

    Modulus Math     97

    Encryption Code     98

    Decryption Code     102

Summary     104

Chapter 7  Guess the Number     105

Conditional Loops     106

Game Time     111

    The Basic Game     111

    Putting It All Together     116

Summary     120

Chapter 8  Becoming a Coder     121

How Coders Code     122

    Have a Plan     122

    Think Small     123

Game Components     124

    Restricting User Input     125

    Storing User Guesses     128

    Displaying Lists     129

    Masking Characters     131

Summary     136

Chapter 9  Hangman     137

Game Time     138

So How Does It Work?     141

Summary     148

Chapter 10  Keep Going     149

Birthday Countdown     150

    Program Requirements     150

    Program Flow     150

    Some Tips     151

Tip Calculator     152

    Program Requirements     152

    Program Flow     153

    Some Tips (Pun Intended)     153

Password Generator     154

    Program Requirements     154

    Program Flow     155

    Some Tips     155

Summary     159

PART II:  ON AN ADVENTURE     161

Chapter 11  Getting Func-ky     163

Functions Revisited     164

    Creating a Function     165

    Passing Arguments     167

    Returning Values     171

Summary     175

Chapter 12  Exploring     177

Game Concept     178

    Game Structure     179

    Prompting for Options     181

    Processing Options     182

    Create a Work Folder     183

Game Time     184

Test It     189

Summary     191

Chapter 13  Cleanup Time     193

Optimizing Your Code     194

String Externalization     196

    Creating the Strings File     196

    Using Externalized Strings     200

Summary     201

Chapter 14  Reduce, Reuse, Recycle, Refactor     203

Understanding Refactoring     204

    Identifying Refactoring Opportunities     205

Creating a User Choice Component     207

    Designing a Reusable Component     208

    Creating the User Options Function     213

    Updating Your Code     218

Summary     221

Chapter 15  Carrying (and Using) Stuff     223

Planning the Inventory System     224

    Creating a Dictionary     225

    Working with Dictionaries     226

    Lists of Dictionaries     228

The Inventory System     229

    Creating an Inventory     230

    Plugging In the Inventory System     232

    Using the Inventory System     233

    Displaying the Inventory     238

Summary     239

Chapter 16  Keeping It Classy     241

The Player System     242

Creating a Player Class     243

    Creating the Class     243

    Defining Properties     244

    Creating Methods     247

    Initializing the Class     250

Using Our New Class     251

Summary     255

Chapter 17  Color Your World     257

Installing Third-Party Libraries     258

Using Colorama     259

    Importing and Initializing the Library     259

    Coloring Your Output     260

Summary     264

Chapter 18  Keep Going     265

Health and Lives     266

Shopping for Items     271

Random Events     275

Battling Enemies     277

Saving and Restoring     280

Summary     282

PART III:  RACING AROUND     283

Chapter 19  Crazy Driver     285

Introducing Pygame     286

Prepping the Game     286

    Game Concept     286

    Installing Pygame     288

    Creating Work Folders     288

    Obtaining Images     289

Getting Started     289

    Initializing Pygame     290

    Displaying Stuff     294

The Game Loop     295

Summary     300

Chapter 20  Image-ine the Possibilities     301

Files and Folders     302

Setting the Background     305

Placing the Cars     310

Summary     317

Chapter 21  We Like to Move It     319

Moving the Enemy     320

Moving the Player     323

Summary     327

Chapter 22  Crash, Bang, Boom     329

You Crashed, Game Over     330

Tracking Score     332

Increasing Difficulty     334

Summary     336

Chapter 23  Finishing Touches     337

Game Over Revisited     338

Pause     341

Varying Enemies     343

Ice Cubes     348

Summary     351

Chapter 24  Keep Going     353

Splash Screen     354

Scores and High Scores     354

Oil Slick     356

Multiple Enemies     357

And Then     358

Summary     359

What Next?     361

There's a Lot More to Python     362

Web Development     362

Mobile App Development     364

Game Development     364

And Then     365

Index     367

Bonus Online Chapter 25  Tinkering, Testing, and Debugging Revisited     (Online Only)

Need help? Get in touch