bubbles8935716
Posts: 2
Joined: Mon Jun 01, 2015 11:40 pm

Invalid Syntax in Python

Mon Jun 01, 2015 11:47 pm

So, I'm trying to run my first Python code:

print("Hello, world!")

in Python 3, and I saved it as hello.py. But when I type in:

python3 hello.py

into LXTerminal, I get:

File "hello.py", line 1
Python 3.2.3 (default, Mar 1 2013, 11:53:50)


(BTY, there's an arrow thingy under the 2)

Can anyone tell me what's wrong?

User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

Re: Invalid Syntax in Python

Tue Jun 02, 2015 1:35 am

I use nano

Code: Select all

nano hello.py

Code: Select all

print('Hello, world!')
Then do python3 hello.py
and get Hello, world!
BTW you can use single quotes as well. :mrgreen:
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Invalid Syntax in Python

Tue Jun 02, 2015 5:13 am

It looks like your file may not contain what you think it does.

Can you type in

Code: Select all

cat hello.py
into LXTerminal and post what it shows. You should get something like this

Code: Select all

pi@raspi2 ~ $ cat hello.py
print("Hello, world!")
pi@raspi2 ~ $ 
You are using an old version of python, but that shouldn't cause that problem.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Invalid Syntax in Python

Tue Jun 02, 2015 5:29 am

bubbles8935716 wrote:Can anyone tell me what's wrong?
My guess is that you clicked on the menu entry for Python3. This will have started the Idle3 IDE. You then typed your code into the resulting Python Shell window and saved it. If you examine your code it probably looks like this:

Code: Select all

pi@RPi2B ~ $ cat hello.py
Python 3.2.3 (default, Mar  1 2013, 11:53:50)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> print("hello!")
hello!
>>>
This will, as you have found, not run correctly when you pass it to the python3 interpreter.

If you want to use Idle3 as the editor for your python scripts, DO NOT type them in the Python Shell window. Use the FILE menu in that screen to OPEN a New Window - initially headed Untitled - and use that window to create and save your script.

bubbles8935716
Posts: 2
Joined: Mon Jun 01, 2015 11:40 pm

Re: Invalid Syntax in Python

Tue Jun 02, 2015 2:57 pm

OK, I realize what I did wrong. I kept on forgetting to write code in a NEW window. Thanks for the help!

User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

Re: Invalid Syntax in Python

Tue Jun 02, 2015 6:09 pm

If you are going to make that mistake forget about using "Idle3 editor" and use something else like "nano" :mrgreen:
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

Return to “Beginners”