OLDDOG
Posts: 6
Joined: Sun Sep 20, 2015 1:08 pm

"EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 1:31 pm

Dear All
I have just bought a PY B+ and along with some electronic kit and the following was part of the first attempt at programming using python3.
Can anyone help with the syntax error i get with Keyboardinterrupt. It is typed the same as the guide book.
Thanks in advance for helping an olddog learning new tricks.

try:
while true
print("..led on")
GPIO.output(LedPin, GPIO.LOW)
time.sleep(0.5)
print("Led off")
GPIO.output(LedPin, GPIO.High)
time.sleep(0.5)

Except KeyboardInterrupt:
GPIO.output (LEDPin, GPIO.High)
GPIO.cleanup ()

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 2:10 pm

Python (and Linux) is case sensitive.

The keyword is except not Except.

It is best to post code within

Code: Select all

 
quotes to preserve the spacing.

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 2:15 pm

Oops, joan beat me to it.

Please put your code inside the tags:

Code: Select all

try: 
    while true
        print("..led on")
        GPIO.output(LedPin, GPIO.LOW)
        time.sleep(0.5)
        print("Led off")
        GPIO.output(LedPin, GPIO.High)
        time.sleep(0.5)
Except KeyboardInterrupt: 
    GPIO.output (LEDPin, GPIO.High)
    GPIO.cleanup ()
I think the problem is using GPIO.High not GPIO.HIGH, and Except not except.

If that doesn't fix it, please post the specific error you are seeing.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 2:18 pm

Mind you the while true line would also fail.

It's best if you cut & paste code so that we are sure what we are reading is accurate.

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 4:04 pm

joan wrote:Mind you the while true line would also fail.

It's best if you cut & paste code so that we are sure what we are reading is accurate.
D'oh, missed that - thanks joan. So I think this should work, but as you say, it would be best if the OP posted their own code.

Code: Select all

try: 
    while True:
        print("..led on")
        GPIO.output(LedPin, GPIO.LOW)
        time.sleep(0.5)
        print("Led off")
        GPIO.output(LedPin, GPIO.HIGH)
        time.sleep(0.5)
except KeyboardInterrupt: 
    GPIO.output (LEDPin, GPIO.HIGH)
    GPIO.cleanup ()
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

OLDDOG
Posts: 6
Joined: Sun Sep 20, 2015 1:08 pm

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 7:18 pm

Thanks all for your help. I will take a look at your replies. Might take me a while to break through the crusty brain cell and get a back to you work in progress for this evening.
In the meantime I have cut and pasted the code from the Python 3 window. I hope this helps cast a better light on the subject.
Many thanks again for your help.

import RPI.GPIO as GPIO
import time

ledpin = 11

GPIO.setmode(GPIO.BOARD)
GPIO.setup(LedPin, GPIO.OUT)
GPIO.output(LedPin, GPIO.HIGH)

try:
while true:
print ("..led on ")
GPIO.output(LedPin, GPIO.LOW)
time.sleep(0.5)
print ("Led off")
GPIO.output(LedPin, GPIO.HIGH)
time.sleep(0.5)

Except KeyboardInterrupt:
GPIO.output (LedPin, GPIO.HIGH)
GPIO.cleanup ()

OLDDOG
Posts: 6
Joined: Sun Sep 20, 2015 1:08 pm

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 8:06 pm

Dear all,
I have i think updated my code as suggested. The error statement when i try to run the code is, "invalid syntax" . A little arrow points the "except" statement. I have tidied the code up as follows. I hope this is correct,

import RPI.GPIO as GPIO
import time

ledpin = 11

GPIO.setmode(GPIO.BOARD)
GPIO.setup(LedPin, GPIO.OUT)
GPIO.output(LedPin, GPIO.HIGH)

try:
while true:
print ("..led on ")
GPIO.output(LedPin, GPIO.LOW)
time.sleep(0.5)
print ("Led off")
GPIO.output(LedPin, GPIO.HIGH)
time.sleep(0.5)

Except KeyboardInterrupt:
GPIO.output (LedPin, GPIO.HIGH)
GPIO.cleanup ()

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 8:08 pm

OLDDOG wrote:Dear all,
I have i think updated my code as suggested. The error statement when i try to run the code is, "invalid syntax" . A little arrow points the "except" statement. I have tidied the code up as follows. I hope this is correct,

import RPI.GPIO as GPIO
import time

ledpin = 11

GPIO.setmode(GPIO.BOARD)
GPIO.setup(LedPin, GPIO.OUT)
GPIO.output(LedPin, GPIO.HIGH)

try:
while true:
print ("..led on ")
GPIO.output(LedPin, GPIO.LOW)
time.sleep(0.5)
print ("Led off")
GPIO.output(LedPin, GPIO.HIGH)
time.sleep(0.5)

Except KeyboardInterrupt:
GPIO.output (LedPin, GPIO.HIGH)
GPIO.cleanup ()
Except cannot be capitalized!
And true has to be capitalized!
and please post your code in code tags, in the top bar.
There are 10 types of people: those who understand binary and those who don't.

OLDDOG
Posts: 6
Joined: Sun Sep 20, 2015 1:08 pm

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 10:49 pm

Dear All,
I think this is correctly uploaded with tags and corrected syntax.
The error is the same. "invalid syntax"

[import RPI.GPIO as GPIO
import time

LedPin = 11

GPIO.setmode(GPIO.BOARD)
GPIO.setup(LedPin,GPIO.OUT)
GPIO.output(LedPin,GPIO.High)

try:
while True:
print ("..led on ")
GPIO.output(LedPin,GPIO.LOW)
time.sleep(0.5)
print ("Led off")
GPIO.output(LedPin,GPIO.High)
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.output (LedPin,GPIO.High)
GPIO.cleanup ()]

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 10:52 pm

You need to edit your post and put the script within code quotes. The easiest way (in the edit window) is to highlight the script text and then press the Code button at the top of the edit window.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Sun Sep 20, 2015 10:58 pm

OLDDOG wrote:Dear All,
I think this is correctly uploaded with tags and corrected syntax.
The error is the same. "invalid syntax"

Code: Select all

import RPI.GPIO as GPIO
import time

LedPin = 11

GPIO.setmode(GPIO.BOARD)
GPIO.setup(LedPin,GPIO.OUT)
GPIO.output(LedPin,GPIO.High)

try:
    while True:
        print ("..led on ")
        GPIO.output(LedPin,GPIO.LOW)
        time.sleep(0.5)
        print ("Led off")
        GPIO.output(LedPin,GPIO.High)
        time.sleep(0.5)
        except KeyboardInterrupt:
            GPIO.output (LedPin,GPIO.High)
            GPIO.cleanup ()
You really need to wrap your code in [code]python stuff goes here ... [/code] tags not in plain square brackets [] .

Here's a version with your five bugs knocked out. The try and except blocks need to be at the same indentation level.

Code: Select all

import RPi.GPIO as GPIO
import time

LedPin = 11

GPIO.setmode(GPIO.BOARD)
GPIO.setup(LedPin,GPIO.OUT)
GPIO.output(LedPin,GPIO.HIGH)

try:
    while True:
        print ("..led on ")
        GPIO.output(LedPin,GPIO.LOW)
        time.sleep(0.5)
        print ("Led off")
        GPIO.output(LedPin,GPIO.HIGH)
        time.sleep(0.5)
except KeyboardInterrupt:
    GPIO.output (LedPin,GPIO.HIGH)
    GPIO.cleanup ()
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

OLDDOG
Posts: 6
Joined: Sun Sep 20, 2015 1:08 pm

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Tue Sep 22, 2015 9:57 am

Code: Select all

import RPi.GPIO as GPIO
import time

LedPin = 11

GPIO.setmode(GPIO.BOARD)
GPIO.setup(LedPin,GPIO.OUT)
GPIO.output(LedPin,GPIO.HIGH)

try:
    while True:
        print ("..led on ")
        GPIO.output(LedPin,GPIO.LOW)
        time.sleep(0.5)
        print ("Led off")
        GPIO.output(LedPin,GPIO.HIGH)
        time.sleep(0.5)
except KeyboardInterrupt:
    GPIO.output (LedPin,GPIO.HIGH)
    GPIO.cleanup ()

OLDDOG
Posts: 6
Joined: Sun Sep 20, 2015 1:08 pm

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Tue Sep 22, 2015 10:03 am

Dear All,
Thanks for the help and instruction submitting the tags. I am gradually seeing the light excuse the pun.
The code worked and i have enjoyed my first success.

Return to “Beginners”