Shane Brown Shane Brown - 1 month ago 10
Python Question

RaspberryPi Python IndentationError: expected an indented block

When running the code below I get an indentation error but I can't seem to find the issue. I'm new to python so I'm sure it's something glaringly obvious but I can't see it.

#Import modules to send commands to GPIO pins
from subprocess import call
import RPi.GPIO as gpio
import time

#Define function to keep script running
def loop():
while True:
time.sleep(0.2)

#Define function to run when interrupt is called
def shutdown(pin):
call('halt', shell=False)

GPIO.setmode(GPIO.BOARD) #Set pin numbering to board numbering
GPIO.setup(7, GPIO.IN) #Set pint 7 as input pin
GPIO.add_event_detect(7. GPIO.RISING, callback=shutdown, bouncetime=200) #Setup inteript to look button press

loop()


When run I get this error:

File "/home/pi/PiSupply/softshut.py", line 8
while True:
^
IndentationError: expected an indented block


Please help, I've spent far too long on this and I can't seem to find the indentation error it's referring to.

Thanks in advance.

Answer
def loop():
    while True:
        time.sleep(0.2)

After a function, you need to indent your code. Same on shutdown function.

Comments