[Tutor] Newbie question: programs not printing

Alan Gauld alan.gauld at btinternet.com
Thu Mar 10 17:00:23 EST 2016


On 10/03/16 13:33, Patrick Craine wrote:

> I downloaded Python 2.7.11 but for some reason it seems that it's not
> responding the way it¹s supposed to. I haven¹t been able to figure out the
> problem. I¹m on Mac OSX 10.11.3.

You probably didn't need to download Python because it is
installed by default on a Mac. Not to worry.

> Here¹s an example of what¹s happening:
> 
>>>> x = int(raw_input('Enter an integer: '))
> if x%2 == 0:
> print 'Even'
> else:
> print 'Odd'
> if x%3 != 0:
> print 'And not divisible by 3'
> Enter an integer: 3
>>>>

Now that looks very strange to me. It starts off with
the Python  >>> prompt but you should have got the
prompt immediately after the first line not at the end.
How are you entering the text, are you using the command
line tool in a Terminal? Or are you using an IDE like IDLE?

Marc has also commented on the lack of indentation(spacing).
Python is quite fussy about indenting code you need to
follow the visual layout from your tutor.

> When I put the integer in, I understand it¹s supposed to return ³odd² or
> ³even,² but it¹s not giving me anything.

That's because somehow the prompt is happening after
you have already executed all the logic code. If I
enter it on my (Linux) PC I get:

>>> x = int(raw_input('Enter an integer: '))
Enter an integer: 3
>>> if x%2 == 0:
...    print 'Even'
... else:
...    print 'Odd'
...
Odd
>>> if x%3 != 0:
...    print 'And not divisible by 3'
...
>>>

Can you see the differences?
The input prompt appears right at the top.

> Also, at the top of the shell I¹m getting this message:
> WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.
> Visit http://www.python.org/download/mac/tcltk/ for current information.

Just ignore that for now, it only affects you once you
get to writing GUIs, which is a long way down the road yet ;-)

The other possible way to run your code is to type it all
into a file using Textedit (or some other text editor,
not a word processor) and save it as plain text in a
folder of your choice (but a new one for your programs
might be a good idea!) as a file called something
like testnum.py

You can then run the file by starting the Terminal application.
Then drag the folder you used to save the file into the Terminal
(If I recall correctly that should change the active Terminal
folder to that one! If not, type cd followed by the folder path)
Then type, at the prompt:

python testnum.py

Hopefully that works.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list