Python Programming, invalid Syntax


17 posts
by khse » 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()
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm
by AussieSusan » 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
Posts: 29
Joined: Tue Aug 28, 2012 10:48 pm
by gkreidl » 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
Posts: 5117
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany
by bensimmo » 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)
Posts: 1450
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire
by khse » 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 349 times
image1.JPG
image1.JPG (22.05 KiB) Viewed 349 times
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm
by KLL » 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
https://www.raspberrypi.org/forums/viewtopic.php?p=1132546#p1132546
looks like http://kll.engineering-news.org/kllfusion01/downloads/LXDE_screen_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.
Posts: 770
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by KLL » Mon Jul 17, 2017 8:41 am
http://kll.engineering-news.org/kllfusion01/downloads/IDLE3_printscreen1.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..
Posts: 770
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by khse » 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!!
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm
by B.Goode » 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/astro-pi-guide/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:
Posts: 3876
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK
by khse » 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!
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm
by gordon77 » 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 ?
Posts: 2905
Joined: Sun Aug 05, 2012 3:12 pm
by khse » 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.
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm
by B.Goode » 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.
Posts: 3876
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK
by gordon77 » 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
Posts: 2905
Joined: Sun Aug 05, 2012 3:12 pm
by khse » Mon Jul 17, 2017 11:59 am
Ok perfect, now it works fine. Thank you guys!
Posts: 8
Joined: Sun Jul 16, 2017 4:11 pm
by bensimmo » Mon Jul 17, 2017 3:07 pm
KLL wrote:http://kll.engineering-news.org/kllfusion01/downloads/IDLE3_printscreen1.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)
Posts: 1450
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire
by KLL » 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/kllfusion01/downloads/RPI_sensehat_emu.jpg
Posts: 770
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand