Topics Covered in This Chapter:: Program
Topics Covered in This Chapter:: Program
Hello! This is a book that will teach you how to program by showing you how to create
computer games. Once you learn how the games in this book work, you'll be able to create
your own games. All you'll need is a computer, some software called the Python
Interpreter, and this book. The software you'll need is free and you can download it from
the Internet.
When I was a kid, I found a book like this that taught me how to write my first programs
and games. It was fun and easy. Now as an adult, I still have fun programming computers,
and I get paid for it. But even if you don't become a computer programmer when you grow
up, programming is a useful and fun skill to have.
Computers are very useful machines. The good news is that learning to program a
computer is easy. If you can read this book, you can program a computer. A computer
program is just a bunch of instructions run by a computer, just like a storybook is just a
whole bunch of sentences read by the reader.
These instructions are like the turn-by-turn instructions you might get for walking to a
friend's house. (Turn left at the light, walk two blocks, keep walking until you find the first
blue house on the right.) The computer follows each instruction that you give it in the order
that you give it. Video games are themselves nothing but computer programs. (And very
1
In this book, any words you need to know will look like this. For example, the word
"program" is defined in the previous paragraph.
In order to tell a computer what you want it to do, you write a program in a language that
the computer understands. The programming language this book teaches is named Python.
There are many different programming languages including BASIC, Java, Python, Pascal,
Haskell, and C++ (pronounced, "c plus plus").
When I was a kid most people learned to program in BASIC as their first language. But
new programming languages have been invented since then, including Python. Python is
even easier to learn than BASIC and it's a serious programming language used by
professional computer programmers. Many adults use Python in their work (and when
programming just for fun).
The first few games we'll create together in this book will probably seem simple
compared to the games you've played on the Xbox, Playstation, or Wii. They don't have
fancy graphics or music but that's because they're meant to teach you the basics. They're
purposely simple so that we can focus on learning to program. Games don't have to be
complicated to be fun. Hangman, Tic Tac Toe, and making secret codes are simple to
program but are also fun.
We'll also learn how to make the computer solve some math problems in the Python
shell. (Don't worry if you don't know a lot of mathematics. If you know how to add and
multiply, you know enough math to do programming. Programming is more about problem
solving in general than it is about solving math problems.)
1 - Installing Python
page, then look for the file called Python 3.1 Windows Installer (Windows binary -does not include source) and click on its link to download Python for Windows.
Figure 1-1: Click the Windows installer link to download Python for Windows from http://www.python.org
Double-click on the python-3.1.msi file that you've just downloaded to start the Python
installer. (If it doesn't start, try right-clicking the file and choosing Install.) Once the
installer starts up, click the Next button and just accept the choices in the installer as you go
(no need to make any changes). When the install is finished, click Finish.
Important Note! Be sure to install Python 3, and not Python 2. The programs in this
book use Python 3, and you'll get errors if you try to run them with Python 2.
The installation for Mac OS is similar. Instead of downloading the .msi file from the
Python website, download the .dmg Mac Installer Disk Image file instead. The link to this
file will look something like "Mac Installer disk image (3.1.1)" on the "Download Python
Software" web page.
If your operating system is Ubuntu, you can install Python by opening a terminal
window (click on Applications > Accessories > Terminal) and entering sudo apt-get
install python3 then pressing Enter. You will need to enter the root password to
install Python, so ask the person who owns the computer to type in this password.
There may be a newer version of Python available than 3.1. If so, then just download the
latest version. The game programs in this book will work just the same. If you have any
problems, you can always Google for "installing Python on <your operating system's
name>". Python is a very popular language, so you should have no difficulty finding help.
A video tutorial of how to install Python is available from this book's website at
http://inventwithpython.com/videos/.
Starting Python
If your operating system is Windows XP, you should be able to run Python by choosing
Start > Programs > Python 3.1 > IDLE (Python GUI). When it's running it should
looking something like Figure 1-2. (But different operating systems will look slightly
different.)
1 - Installing Python
You do not need to type the "9." on the left side, or the space that immediately follows it.
Just type it like this:
number = random.randint(1, 20)
Those numbers are only used so that this book can refer to specific lines in the code.
They are not a part of the actual program.
Aside from the line numbers, be sure to enter the code exactly as it appears. Notice that
some of the lines don't begin at the leftmost edge of the page, but are indented by four or
eight spaces. Be sure to put in the correct number of spaces at the start of each line. (Since
each character in IDLE is the same width, you can count the number of spaces by counting
the number of characters above or below the line you're looking at.)
For example, you can see that the second line is indented by four spaces because the four
characters ("whil") on the line above are over the indented space. The third line is
indented by another four spaces (the four characters, "if n" are above the third line's
indented space):
5
1 - Installing Python
tutorial of how to use the diff tool is available from this book's website at
http://inventwithpython.com/videos/.
Summary
This chapter has helped you get started with the Python software by showing you the
python.org website where you can download it for free. After installing and starting the
Python IDLE software, we will be ready to learn programming starting in the next chapter.
This book's website at http://inventwithpython.com has more information on each of the
chapters, including an online tracing website that can help you understand what exactly
each line of the programs do.