Page 1 of 1

"EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 1:31 pm
by OLDDOG
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 ()

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 2:10 pm
by joan
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.

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 2:15 pm
by Hove
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.

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 2:18 pm
by joan
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.

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 4:04 pm
by Hove
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 ()

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 7:18 pm
by OLDDOG
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 ()

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 8:06 pm
by OLDDOG
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 ()

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 8:08 pm
by kusti8
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.

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 10:49 pm
by OLDDOG
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 ()]

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 10:52 pm
by joan
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.

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Sun Sep 20, 2015 10:58 pm
by DougieLawson
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 ()

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Tue Sep 22, 2015 9:57 am
by OLDDOG

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 ()

Re: "EXCEPT" SYNTAX ERROR KEYBOARDINTERRUPT

Posted: Tue Sep 22, 2015 10:03 am
by OLDDOG
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.