Save your work

If we leave and reenter the Python interactive interpreter the previously defined names will be lost as shown:

>>> print mpg
Traceback (most recent call last):
File "", line 1, in ?
NameError: name 'mpg' is not defined

That last line is saying that we lost our name mpg.

What programmers do to save their work is to write it in a text editor and then save it to a file.

But only pure text editors can be used to write programs. A pure text editor is one that do not introduce in the text file anything beyond what you type. Microsoft Word is not a pure text editor. If you are using Windows you can use Notepad. For Linux there are many, gedit is one of them.

Close the Python interactive interpreter and create a directory for your programs. Name it whatever you want. For this course I will suppose the name is my_programs.

Now write the next program in a text editor and save the file as mileage.py in the directory you just created.

mpg = 320 / 11.2
distance = 632
gallon_price = 2.41
make = "Honda"
model = "Accord"

print make, model, "mileage is", mpg, "miles per gallon"
print "Total trip cost is US$", distance / mpg * gallon_price

computer programming degree learning course tutorial