Question: I'm writing a program to run on my Raspberry Pi, and I can't seem to get past this pesty syntax error. Here's my code:

```
import RPi.GPIO as GPIO, time

GPIO.setmode(GPIO.BCM)

GPIO.setup(14,GPIO.OUT)

GPIO.output(14,GPIO.HIGH)

def RCtime (PiPin):
  measurement = 0
  # Discharge capacitor
  GPIO.setup(PiPin, GPIO.OUT)
  GPIO.output(PiPin, GPIO.LOW)
  time.sleep(0.1)

  GPIO.setup(PiPin, GPIO.IN)
  # Count loops until voltage across
  # capacitor reads high on GPIO
  while (GPIO.input(PiPin) == GPIO.LOW):
    measurement += 1

  return measurement

# Main program loop
while True:
  print RCtime(4) # Measure timing using GPIO4

except KeyboardInterrupt:
  GPIO.cleanup()

```

Returns the following error:

```
File "measure.py", line 28
    except KeyboardInterrupt:
         ^
SyntaxError: invalid syntax

```

I can't seem to find the problem. Can anyone help?

Answer: 
Since the term is called a try...except statement, you must have a `try` keyword. Wrap the try...except around the lines you wanted to error handle. Note: you should wrap as less as possible: 

```
while True:
  try:
    print RCtime(4) # Measure timing using GPIO4
  except KeyboardInterrupt:
    break # break the while loop
  finally:
    GPIO.cleanup() # GPIO clean up

```

Edit: as suggested, the GPIO cleanup should be ran regardless if there's an exception, you should place the cleanup operation inside the finally clause.
You should put your function in the `try` block:

```
# Main program loop
try:
    while True:
        print RCtime(4) # Measure timing using GPIO4
except KeyboardInterrupt:
    GPIO.cleanup()

```

I think this will work.