[Tutor] Could you look over my code?

Steven D'Aprano steve at pearwood.info
Mon Jan 5 03:39:57 CET 2015

Hi Connor, and welcome. My response is below.

On Sun, Jan 04, 2015 at 04:13:50PM +0000, Connor Perkis wrote:

> Hello, I am currently learning python and I have a book to assist me 
> called ‘Python Programming For The Absolute Beginner (3E)’ and I’ve 
> just read the first chapter. I typed the code. Would you be able to 
> help me with:
> 1.How to execute .py files properly on Mac OSX Yosemite

Good question! I don't have a Mac, so I'm going to have to guess.

I *guess* that the simplest way is to use the command line. To do that, 
you need to run the Terminal application. Here is a video that claims to 
show you how to do that, although I have not watched it:


Here are some more instructions:


Once you have the terminal open, you will be presented with a command 
line prompt. It may look something like:

MyMacPro:~ connor$

Notice the dollar sign at the end? That tells you that you are in the 
system shell, not python. Now you can run Python: type the following 
command, and press the ENTER key at the end:


You might need to specify a version number, say:


but I don't know what version you have installed.

This will get you into the Python interactive interpreter, sometimes 
called the REPL ("Read, Eval, Print, Loop"). You can tell you are in the 
Python interactive interpreter, because the prompt will change to >>> 
(three greater-than symbols) instead.

The interactive interpreter is really useful for using Python as a 
calculator, or to test small pieces of code. To exit the Python 
interpreter when you are finished, enter:


and you will be returned to the system shell with its $ prompt.

Do that now, because we're not going to use Python interactively. 
Instead, let's run your file.

At the system prompt, type:

python path/to/my/script.py

then press ENTER. Again, you might need to include the version number:

python2.7 path/to/my/script.py

for example. Very important, don't write *literally* 
"path/to/my/script.py", that probably won't work. You need to specify 
the pathname to your file. How do you do that?

If your file is called "game.py" (it should end with .py) and it is 
inside a folder called "projects", which is inside your home directory, 
then you would type:


Adjust as needed depending on which folders (if any) you use and the 
name of the file.

If you have trouble with this bit, tell us:

- the version number of Python you are using
- the name of the file
- where it is (the name of each folder it is inside)

and we ought to be able to work out what command line you need to use.

I strongly recommend that you learn how to use the command line, because 
when it comes to programming it will be very useful. But there are other 
alternatives, such as IDLE, and the OS X Finder. You can read more 
instructions here, and a few videos (which I haven't watched):



If you're still having trouble after reading and/or watching these, 
please feel free to ask for more help.

> 2. Have a look at this code fro the book and tell me what to improve 
> (This is a program where it says game over, and it waits for the user 
> to press the enter key to quit:
> print(“Game Over”)
> input(“\n\nPress the enter key to exit”)

Are you using Python 3 or better? If so, that code is absolutely fine 
and will work perfectly. It is so short and sweet that there is nothing 
to improve.

If you are using Python 2, you may find that it gives you an error, 
which will probably look like this:

SyntaxError: unexpected EOF while parsing

Don't be distressed! You've just stumbled across a small difference 
between Python version 2 and version 3. In Python 3, you should use 
"raw_input" instead of "input", so your code will look like this:

print("Game Over")
raw_input("\n\nPress the enter key to exit")


More information about the Tutor mailing list