iInvent Your OwnComputer Gameswith Python, 2ndEditionBy Al Sweigart
iiCopyright 2008-2012 by Albert SweigartSome Rights Reserved. "Invent Your Own Computer Games with Python" ("Invent with Python")is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United StatesLicense.You are free:To Share — to copy, distribute, display, and perform the workTo Remix — to make derivative worksUnder the following conditions:Attribution — You must attribute the work in the manner specified by the author orlicensor (but not in any way that suggests that they endorse you or your use of the work).(Visibly include the title and author's name in any excerpts of this work.)Noncommercial — You may not use this work for commercial purposes.Share Alike — If you alter, transform, or build upon this work, you may distributethe resulting work only under the same or similar license to this one.This summary is located here: s/Your fair use and other rights are in no way affected by the above. There is a human-readablesummary of the Legal Code (the full license), located 3.0/us/legalcodeBook Version 31If you've downloaded this book from a torrent, it’s probably out of date. Goto http://inventwithpython.com to download the latest version instead.ISBN 978-0-9821060-1-32.1 Edition
ivFor Caro, with more lovethan I ever knew I had.
vA Note to Parents andFellow ProgrammersThank your for reading this book. My motivation for writing this book comes from a gap I saw intoday's literature for kids interested in learning to program. I started programming when I was 9years old in the BASIC language with a book similar to this one. During the course of writingthis, I've realized how a modern language like Python has made programming far easier andversatile for a new generation of programmers. Python has a gentle learning curve while stillbeing a serious language that is used by programmers professionally.The current crop of programming books for kids that I've seen fell into two categories. First,books that did not teach programming so much as "game creation software" or a dumbed-downlanguages to make programming "easy" (to the point that it is no longer programming). Orsecond, they taught programming like a mathematics textbook: all principles and concepts withlittle application given to the reader. This book takes a different approach: show the source codefor games right up front and explain programming principles from the examples.I have also made this book available under the Creative Commons license, which allows you tomake copies and distribute this book (or excerpts) with my full permission, as long as attributionto me is left intact and it is used for noncommercial purposes. (See the copyright page.) I want tomake this book a gift to a world that has given me so much.Thank you again for reading this book, and feel free to email me any questions or comments.Al Sweigartal@inventwithpython.comThe full text of this book is available in HTML or PDF format at:http://inventwithpython.com
viWho is this book for?Programming isn't hard. But it is hard to find learning materials that teach you to do interestingthings with programming. Other computer books go over many topics that most newbie codersdon't need. This book will teach you how to program your own computer games. You will learn auseful skill and have fun games to show for it! This book is for: Complete beginners who wants to teach themselves computer programming, even if theyhave no previous experience programming.Kids and teenagers who want to learn computer programming by creating games. Kids asyoung as 9 or 10 years old should be able to follow along.Adults and teachers who wish to teach others programming.Anyone, young or old, who wants to learn how to program by learning a professionalprogramming language.
viiTABLE OF CONTENTSInstalling Python . 1Downloading and Installing Python. 2Windows Instructions . 3Mac OS X Instructions. 4Ubuntu and Linux Instructions . 4Starting Python. 4How to Use This Book. 5The Featured Programs . 5Line Numbers and Spaces . 6Text Wrapping in This Book . 6Tracing the Program Online . 7Checking Your Code Online . 7Summary . 7The Interactive Shell . 8Some Simple Math Stuff . 8Integers and Floating Point Numbers. 9Expressions . 10Evaluating Expressions . 11Expressions Inside Other Expressions . 12Storing Values in Variables . 12Using More Than One Variable . 15Overwriting Variables . 16Summary . 17Strings . 18Strings . 18String Concatenation. 19
viiiWriting Programs in IDLE's File Editor . 20Hello World! . 20hello.py . 21Saving Your Program . 22Opening The Programs You've Saved . 23How the “Hello World” Program Works . 24Comments . 24Functions . 25The print() function . 25The input() function . 25Ending the Program . 26Variable Names . 27Summary . 27Guess the Number . 29The “Guess the Number” Game. 29Sample Run of “Guess the Number” . 30Guess the Number's Source Code . 30The import statement . 32The random.randint() function . 33Calling Functions that are Inside Modules . 35Passing Arguments to Functions . 35Welcoming the Player . 36Loops . 37Blocks . 37The Boolean Data Type . 38Comparison Operators . 38Conditions . 39Experiment with Booleans, Comparison Operators, and Conditions. 39Looping with while statements . 41
ixThe Player Guesses . 43Converting Strings to Integers with the int() function . 43Incrementing Variables . 45if statements . 45Is the Player's Guess Too Low? . 45Is the Player's Guess Too High? . 47Leaving Loops Early with the break statement. 47Check if the Player Won . 48Check if the Player Lost . 48Summary: What Exactly is Programming? . 49A Web Page for Program Tracing. 50Jokes. 52Making the Most of print() . 52Sample Run of Jokes . 52Joke's Source Code . 53How the Code Works . 53Escape Characters . 54Some Other Escape Characters . 54Quotes and Double Quotes . 55The end Keyword Argument .
books that did not teach programming so much as "game creation software" or a dumbed-down languages to make programming "easy" (to the point that it is no longer programming). Or second, they taught programming like a mathematics textbook: all principles and concepts with little application given to the reader. This book takes a different approach: show the source code for games right up front .