khse
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm

Python Programming, invalid Syntax

Sun Jul 16, 2017 4:29 pm

I recently just got my raspberry model b with sensehat addon. This is my first time using such programming devices as python and I don't get why there is always a syntax error.
I'm writing my command in Python 3 (IDLE), run it (working) for one time, save it as a .py file. When I try to run the command again, a pop-up showing invalid syntax appears and I can't run it again. The LED screen of the Sensehat is stuck at that moment and I have to rewrite the whole command to run it again for one time.. Does anybody know why this is happening?

Command:
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "copyright", "credits" or "license()" for more information.

from sense_hat import SenseHat
from time import sleep
from random import randint

sense = SenseHat()

r = randint(0,255)
sense.show_letter("O", (r,0,0))
sleep(1)

r = randint(0,255)
sense.show_letter("M",(0,r,0))
sleep(1)

r = randint(0,255)
sense.show_letter("G",(0,0,r))
sleep(1)
sense.clear()

AussieSusan
Posts: 29
Joined: Tue Aug 28, 2012 10:48 pm

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 2:56 am

If you are running IDLE, then the cursor will be placed in your source file at the point where the error was found. It should also have a highlight at the spot.
That should help you at least locate where the compiler is having problems.
Susan

gkreidl
Posts: 5207
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 4:47 am

Is ist possible that you are typing your code into Idle's shell window instead or using an editor window?
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

bensimmo
Posts: 1696
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 6:45 am

There are three places to run Python3 in Raspian now
IDLE3 which is where I assume you are editting and typing if you are following the sites Learn the SenseHAT lessons. You 'edit and run' the program from there.
You have a text editor window open and another IDLE3 terminal window that show you what is happening

The terminal window (black rectangular box for a command line) where you would run it as
Python3 theProgram.py


Now there is Thonny too, excellent program for learning on and learning to debug and follow how a program is running.


(Perhaps screenshot, though no idea if the usual prints teen key work in Raspbian desktop, there is no screenshot utility installed by default like Windows Snipping Tool and then you have to find a way of getting a picture up on here, it doesn't like 'normal size' pictures)

khse
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 7:43 am

The invalid Syntax is always at the Beginning:
Python 3.4.2 <-- the "4" is highlighted.

I'm not sure where I'm trying to run the command. Well I just click on the Python 3 Icon and start writing my command, when I press enter at the end, the commands starts running for one time. Then I'm saving the file on the desktop. Reopening it, you get one more option in the topbar of the file, I can "Run" the file pressing F5. But it won't work.
Attachments
image2.JPG
image2.JPG (21.42 KiB) Viewed 684 times
image1.JPG
image1.JPG (22.05 KiB) Viewed 684 times

User avatar
KLL
Posts: 987
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 7:44 am

bensimmo wrote:Perhaps screenshot, though no idea if the usual prints teen key work in Raspbian desktop, there is no screenshot utility installed by default like Windows Snipping Tool and then you have to find a way of getting a picture up on here, it doesn't like 'normal size' pictures)
raspbian has SCROT
and already linked to [Print Screen]
can change like i did to also have print active window [Alt][Print] see
viewtopic.php?p=1132546#p1132546
looks like http://kll.engineering-news.org/kllfusi ... _print.jpg
possibly add a upload to dropbox...
Last edited by KLL on Mon Jul 17, 2017 8:59 am, edited 1 time in total.

User avatar
KLL
Posts: 987
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 8:41 am

http://kll.engineering-news.org/kllfusi ... creen1.jpg
here i used IDLE3 but NO HAT, so the program can not work.

so that is how the IDLE3 file edit window and the IDLE3 shell window can look.

here i operate via VNC / screen shot on win7 PC / upload server..

khse
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 9:17 am

I'm not getting it, so sorry. :/ What do I need to do that it works? The Pi and SenseHat is fully working. But thank you for your commitment so far!!

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

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 9:39 am

@gkreidl was most likely correct.

When you start up IDLE3, ignore the initial default Python Shell Window.

Open a new window via the File/New menu option.

Retype your script there, or if you wish edit your existing script to remove all the 'junk' that was added by Idle that you did not originally type.

The script should begin at

Code: Select all

from sense_hat import SenseHat
the first 4 lines in your saved file are superfluous.

Edit: added after checking the Raspberry Pi Foundation resource here:
https://www.raspberrypi.org/learning/as ... program.md

The first sentence of that resource says:
Open Python 3 using Menu > Programming > Python 3. This will cause a Python Shell window to appear. Select File > New File, and type in the following code:

khse
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 10:36 am

Thanks, I will try that! I'm currently overwriting and redownloading my SD-Card. I'll keep you updated!

gordon77
Posts: 2952
Joined: Sun Aug 05, 2012 3:12 pm

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 10:48 am

khse wrote:The invalid Syntax is always at the Beginning:
Python 3.4.2 <-- the "4" is highlighted.

I'm not sure where I'm trying to run the command. Well I just click on the Python 3 Icon and start writing my command, when I press enter at the end, the commands starts running for one time. Then I'm saving the file on the desktop. Reopening it, you get one more option in the topbar of the file, I can "Run" the file pressing F5. But it won't work.
What editor are you using ? It's hard to make out your screenshot BUT why does it have the bit about python at the top ?
It seems to be seeing that as code ?

You need to open a new window and type your code in that ?

khse
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 10:53 am

The text at the top is there when I click on the Python 3(IDLE) icon to open a new file. I can not delete that apparently.

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

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 11:24 am

khse wrote:The text at the top is there when I click on the Python 3(IDLE) icon to open a new file. I can not delete that apparently.
Clicking on the Python 3 (IDLE3) entry on the Raspbian desktop menus opens a new instance of the IDLE 3 IDE/Editor. It does NOT open a new file.

Please see my reply a couple of hours ago for precise information from the Raspberry Pi Foundation about how to do this correctly.

gordon77
Posts: 2952
Joined: Sun Aug 05, 2012 3:12 pm

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 11:26 am

khse wrote:The text at the top is there when I click on the Python 3(IDLE) icon to open a new file. I can not delete that apparently.
Once opened click on FILE, and then NEW FILE to open a new window, and type in that.

Don't type in the Python 3.4.2 Shell window

khse
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 11:59 am

Ok perfect, now it works fine. Thank you guys!

bensimmo
Posts: 1696
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Python Programming, invalid Syntax

Mon Jul 17, 2017 3:07 pm

KLL wrote:http://kll.engineering-news.org/kllfusi ... creen1.jpg
here i used IDLE3 but NO HAT, so the program can not work.

so that is how the IDLE3 file edit window and the IDLE3 shell window can look.

here i operate via VNC / screen shot on win7 PC / upload server..
Change sense_hat to sense_emu and it should work (on a standard Raspian install)

User avatar
KLL
Posts: 987
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Python Programming, invalid Syntax

Tue Jul 18, 2017 7:59 am

bensimmo wrote:Change sense_hat to sense_emu
yes, but it was not the idea to make that program working, just to show how a file edit window / with his source code / looks like
but thanks for the nice idea!
http://kll.engineering-news.org/kllfusi ... at_emu.jpg

Return to “Beginners”

Who is online

Users browsing this forum: pipuppy and 36 guests