Making Your Own Program

A program is just a file with code in it. Let's make one.

  1. Start TextEdit from your Applications folder.
  2. Go to the "Format" menu and say "Make Plain Text".
  3. Now type this in:

#!/usr/bin/env python

print 'time to play hangman'

The first line tells the computer which what language the program is written in: python.

After that, you can write as many lines of python code as you like.

Save the file with the name "game.py" on your desktop. Running Your Program

Go back to Terminal. If you are still in python, press control-D to get out. Arrange your windows so you can see TextEdit and Terminal at the same time.

The magic way to make a file into a program is "chmod +x filename" like this:

laptop:~ student$ chmod +x Desktop/game.py laptop:~ student$

The "chmod" command stands for "change mode" and "+x" means "make it executable." You just marked your file as a program.

Now you can run your program by typing its name.

laptop:~ student$ Desktop/game.py time to play hangman laptop:~ student$

That's it. You've made your first program! Programs are just Scripts

A program is just a script that a computer reads as quickly as it can. If we put a lot of lines of code into a program, the computer will do them all, one after the other.

Try typing these extra lines into your TextEdit. Don't forget to save the file again.

#!/usr/bin/env python

print 'time to play hangman' secret = 'crocodile' for letter in secret: print '_',

Now run the program in Terminal.

You can use the up-arrow if you don't want to bother typing the name of your program again. After you have the name of your program, press return.

laptop:~ student$ Desktop/game.py time to play hangman _ _ _ _ _ _ _ _ laptop:~ student$

See what happens? Everthing in your program ran very quickly. Using "if" to Choose

In our hangman game, we should show where any guessed letters are. To decide whether to print a line or a letter, we will need to use "if" and "else".

Try changing your program inside TextEdit like this:

#!/usr/bin/env python

print 'time to play hangman' secret = 'crocodile' guesses = 'aeiou'

for letter in secret: if letter in guesses: print letter, else: print '_',

Don't forget to line everything up, and remember to save it.

What happens when you run it?

The line "if something in something:" makes a choice. If the letter is in our guesses, it prints the letter. Otherwise it prints a little line ("else:" is how you say "otherwise" in python).

Since the whole thing is under the "for something in something", this choice is repeated for every letter.

laptop:~ student$ Desktop/game.py _ _ o _ o _ i _ e laptop:~ student$

Check your spelling and spacing and punctuation if you get errors. Take your time to get it to work.

Next

This topic: Users/Ian > MobilePython > ActivityGame > ActivityGame1 > ActivityGame2 > ActivityGame3 > ActivityGame4 > ActivityGame5
Topic revision: 20 Jan 2012, ian
Contact Us | Section Map | Disclaimer | RSS feed RSS FeedBack to top ^

Valid XHTML and CSS | Built on Foswiki

Page Updated: 20 Jan 2012 by ian. © Victoria University of Wellington, New Zealand, unless otherwise stated