1. Getting Started
Introduction
So youve heard the Internet chanting, Learn to code! Learn to code!, and youve read that Python is a good place to startbut now what? Many people who want to program dont know where to start. The idea that you can create anything with code is paralyzing. This book provides a clear goal: learn Python by creating a text adventure.
This book will teach you the fundamentals of programming, including how to organize code and some coding best practices. By the end of the book, you will have a working game that you can play or show off to friends. You will also be able to change the game and make it your own by writing a different story line, including adding new items, creating new characters, etc.
Learning to program is an exciting endeavor, but can feel daunting at first. However, if you stick with it, you could become a professional programmer or a weekend hobbyist, or both! My story is similar to the stories of many programmers: The first thing I programmed was a number guessing game in QBASIC and now programming is my job. I hope that you, too, can join us, and I thank you for choosing this book as the place to start.
Who This Book Is For
This book is intended for people who have never programmed before or for novice programmers starting out with Python. If youre in this second group, you can probably skim some of the early material.
Although this is geared toward beginners, I do make some assumptions that you know computer basics such as opening a command prompt, installing software, etc. If you get stuck on anything, an Internet search for how to do [thing] on [operating system] will typically help you out. Particularly useful web sites for programmers are StackOverflow ( so if you see them in your search results, give them a shot first.
How To Use This Book
In each chapter of the book, you will make progress on the overall goal of creating your text adventure. The early chapters may seem like slow going because they focus on learning the basics of Python. Things will pick up in the second half of the book, when the focus shifts toward building the game world.
I suggest reading this book on or beside your computer so you can easily go back and forth between reading and writing code. Each of the chapters in the first half of the book will end with a homework section. These problems wont be required for the main game, but you should at least try them. When applicable, solutions are provided at the end of the book.
Most of the Python code in this book will look like this:
1 greeting = "Hello, World!"
2 print (greeting)
Code that is intended to be entered into an interactive Python session (see Chapter ) will look like this:
>>> greeting = "Hello, World!"
>>> print (greeting)
References to code or commands that appear inline will appear like this . Technical terms that you should learn appear like this.
If you ever get stuck, you can download the code for each chapter in the book here. You can also check Appendix B for some common errors you may run into.
Finally, this game is your game. It is fully customizable, and if you feel comfortable adding more rooms and enemies, changing the story, making it more difficult, etc., please do so. I will point out good customization opportunities like this:
Customization Point
Some notes about customization.
Keep in mind that each chapter builds on the last, so if you deviate too far from the material, you may want to save your customized code in another directory so you can keep learning from the source material.
Setting Up Your Workspace
Don't skip this section! You need to make sure everything is set up properly before you begin working on the code in this book. A lot of problems await you if you have an improper configuration.
Python Versions
The creators of Python made a decision that Python 3 would not be backwards compatible with Python 2. And while Python 3 was released in 2008, some people still cling to Python 2. Theres no reason for beginners to start out with Python 2, so this book is written using Python 3. Unfortunately, some operating systems are bundled with Python 2, which can make installing and using Python 3 a bit tricky. If you run into trouble, there are plenty of detailed instructions for your specific operating system online.
Installing Python
There are many ways to install Python depending on your operating system and what (if any) package managers you use.
Windows
An advantage of installing Python on Windows is that you dont need to worry about an already existing old version. Windows does not have a standard package manager, so youll need to download the installer from Python.
Open http://python.org/download/ in your browser and download the latest 3.x.y installer for Windows.
Run the installer.
On the first screen of the installer, you will see an option to include Python 3.X on the PATH. Be sure to check that box.
Proceed through the installation; the default settings are fine. If you see another option in the installer for Add Python to Environment Variables, make sure that box is checked too.
Mac OS X
From my experience, the easiest way to install developer tools on Mac OS X is by using the Homebrew ). However, I appreciate that you may not want to install something to install something else! Ill provide the Homebrew steps first and then the more traditional path.
Using Homebrew:
Open a terminal.
Install Homebrew by running the command at http://brew.sh in the terminal.
Install Python 3 with the following command: brew install python3 .
You will now use the command python3 anytime you want to use Python. The command python points to the default Mac OS X installation of Python, which is version 2.7.5.
Using the Installer:
Open http://python.org/download/ in your browser and download the latest 3.x.y installer for Mac OS X.
Open the download package and then run Python.mpkg .
Follow the installation wizard. The default settings are fine.
You will now use the command python3 anytime you want to use Python. The command python points to the default Mac OS X installation of Python, which is version 2.7.5.
Linux
If youre using Linux, chances are you are already comfortable using your distributions package manager, so I wont go into details. Typically, something like sudo apt-get install python3 or sudo yum install python3 will get what you want. It is also possible that your distribution already includes Python 3. If all else fails, you can download the source and build Python from the official web site (
Verify Your Installation
To verify your installation, open a command prompt/terminal (Ill use console, command prompt, and terminal interchangeably) and try both of these commands:
python version
python3 version
There are four possibilities: