Back to the Game Over Program


So far, you've run a version of the Game Over program through IDLE. While you're in the process of writing a program, running it through IDLE is a fine way to go. But I'm sure you want your finished products to work like any other program on your computer. You want a user to simply double-click your program's icon to launch your program.

If you were to try to run the version of the Game Over program I've shown so far in this way, you'd see a window appear and, just as quickly, disappear. You'd probably think that nothing happened. But something would have happened. It just would have happened too fast for you to notice. The program would run, Game Over would be displayed, and the program would end, all in a split second. What the program needs is a way to keep its console window open.

This updated version of Game Over, the final chapter project, keeps the program window open so the user can see the message. After displaying Game Over, the program also displays the message Press the enter key to exit. Once a user presses the Enter key, the program exits, and the console window disappears.

I'll walk you through the code one section at a time. But I recommend that you load the program from the CD-ROM and take a look at it. Better yet, type in the program yourself and run it.

Using Comments

The following are the first three lines of the program:

 # Game Over # Demonstrates the print command # Michael Dawson - 12/26/02 

These lines aren't statements for the computer to execute. In fact, the computer totally ignores them. These notes, called comments, are for the humans. Comments explain programming code in English (or any other language for that matter). Comments are invaluable to other programmers and help them to understand your code. But comments are also helpful to you. They remind you of how you accomplished something that may not be clear at first glance.

You create a comment with the number sign symbol, #. Anything after this symbol (except in a string) on the rest of the line is a comment. Comments are ignored by the computer. Notice that comments are colored red in IDLE to make them stand out.

It's a good idea to start all of your programs with a few comments, like I did here. It's helpful to list the title of the program, its purpose, the programmer, and the date the program was written.

You may be thinking: "Why have comments at all? I wrote the program, so I know what it does." That may be true a month after you write your code, but experienced programmers know that after a few months away from a program, your original intentions may not be as clear. If you want to modify an old program, a few well-placed comments may make your life much easier.

start sidebar
IN THE REAL WORLD

Comments are even more helpful to another programmer who needs to modify a program you wrote. This kind of situation comes up a lot in the world of professional programming. In fact, it's estimated that 80 percent of a programmer's time and effort go toward maintaining code that already exists. It's not uncommon for a programmer to be charged with the task of modifying a program written by someone else—and there's a chance that the original programmer won't be around to answer any questions. So, good comments are critical.

end sidebar

Using Blank Lines

Technically, the next line in the program is blank. The computer generally ignores blank lines; these, too, are just for the humans reading the code. Blank lines can make programs easier to read. Usually, I keep lines of related code together and separate sections with a blank line. In this program, I separated the comments from the print statement with a blank line.

Printing the String

The next line in the program should seem familiar to you:

 print "Game Over" 

It's your old friend, the print statement. This line, just as it does in interactive mode, prints Game Over.

Waiting for the User

The last line of the program:

 raw_input("\n\nPress the enter key to exit.") 

displays the prompt, Press the enter key to exit. and waits for the user to press the Enter key. Once the user presses the key, the program ends. This is a nice trick to keep a console window open until the user is done with an application.

Normally, this is about the time I'd explain just what is going on in this line. But I'm going to keep you in suspense. Sorry. You'll have to wait until the next chapter to fully appreciate this one line.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net