简体   繁体   English

RaspberryPi Python IndentationError:应为缩进块

[英]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.我是 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()

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.提前致谢。

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

After a function, you need to indent your code.在函数之后,您需要缩进您的代码。 Same on shutdown function. shutdown功能也一样。

You need to indent while True.您需要在 True 时缩进。 In Python you have to indent code that belongs to a definition or loop.在 Python 中,您必须缩进属于定义或循环的代码。 The code in def loop() needs to be indented. def loop() 中的代码需要缩进。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM