[Tutor] Executing "if :" function on terminal

Alan Gauld alan.gauld at yahoo.co.uk
Tue Sep 5 04:46:34 EDT 2017


On 05/09/17 08:56, Nathan Kckaiyer wrote:

> i have just started learning python. I have repeatedly hit a road
> block on my mac os terminal . Everytime i use the "if  :" command i
> get syntax errors. 

The problem is that you are somehow executing the Python
interpreter prompt in the interpreter. WE need to know exactly how you
are running the code.

Its possible you are trying to execute the shell window of IDLE (or some
other IDE) or that you are copy/pasting too much code from one window to
another. But its impossible to guess.

You need to tell us *exactly* how you run your code.
What tools are you using? What buttons/menus you use etc.

>>>> a=2
>>>> if a==2 :
> 	print("correct")

So far so good. That looks like you are typing into the
interactive mode of the Python interpreter. Now if you
just hit Return you should see "correct" being printed
on the screen.

> Solariss-MacBook:~ Solaris$
> /var/folders/Db/DbSciGOcFmyQ3L+HyzOb5E+++TI/-Tmp-/Cleanup\ At\
> Startup/test-526290211.381.py.command ; exit;
>   File "/Users/Solaris/Desktop/test.py", line 1
>     Python 3.0.1 (r301:69597, Feb 14 2009, 19:03:52)
>              ^
> SyntaxError: invalid syntax

But this looks like an OS terminal of some kind trying
to execute a file called test.py

But that file does not contain the Python code above,
it includes the header printed by the interpreter in
interactive mode. That is not executable code and so
Python complains.

test.py should only contain:

a=2
if a==2 :
    print("correct")


and nothing else.

HTH
-- 
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