RaspberryPi Python IndentationError: expected an indented block

RaspberryPi Python IndentationError: expected an indented block

当 运行 执行下面的代码时,出现缩进错误,但我似乎找不到问题所在。我是 python 的新手,所以我确定它非常明显,但我看不到它。

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

当 运行 我得到这个错误:

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

请帮忙,我在这上面花了太长时间,我似乎找不到它所指的缩进错误。

提前致谢。

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

函数后,您需要缩进代码。 shutdown 函数相同。

True 时需要缩进。在 Python 中,您必须缩进属于定义或循环的代码。 def loop()中的代码需要缩进

这意味着编译器希望在单词 while 之前缩进。