indentation error in python for raspberry pi
Question: I am creating a motion sensor with my Raspberry Pi with python. I had this
working fine to just print "motion Detected!", but now I'd like to add a
feature to also take a picture. I am getting an indentation error on the line
`camera = picamera.PiCamera()`. Removing this line causes an indentation error
on the next line. What is going on here? Can my defined callback only be 1
line of code?

    
    
    # coding: utf-8
    import RPi.GPIO as GPIO
    import picamera
    import time
    
    GPIO.setmode(GPIO.BCM)
    
    PIR_PIN = 7
    
    GPIO.setup(PIR_PIN, GPIO.IN)
    
    filename = 0
    
    def MOTION(PIR_PIN):
    
                   print "Motion Detected!"
                   camera = picamera.PiCamera()
                   filename = 'image' + filenumber + '.jpg'
                   camera.capture(filename)
                   filenumber = filenumber + 1
    
    print "PIR Module Test (CTRL+C to exit)"
    
    time.sleep(2)
    
    print "Ready"
    
    try:
    
        GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
    
        while 1:
    
            time.sleep(100)
    
    except KeyboardInterrupt:
    
        print " Quit"
    
        GPIO.cleanup()
        enter code here
    


Answer: When looking at the indentation for the `MOTION` function, this is it (with
tabs being visualized by `\t`):

    
    
    def MOTION(PIR_PIN):
    
                   print "Motion Detected!"
    \t\t\t   camera = picamera.PiCamera()
    \t\t\t   filename = 'image' + filenumber + '.jpg'
    \t\t\t   camera.capture(filename)
    \t\t\t   filenumber = filenumber + 1
    

As you can see, the `print` is indented with spaces, while the rest of the
lines is indented with 3 tabs first and then 3 spaces. So the print and the
following lines have a different indentation which throws Python off.

In Python indentation matters, and for a level of indentation, the indentation
needs to be exactly the same. So if you indent with 15 spaces first, you need
to keep that level for the whole function body.

In general, you should absolutely stay consistent in the way you indent:
Either use tabs, or use spaces. Don’t use both, and especially not on the same
line.

