2016-10-23 56 views
-2

当运行下面的代码时,我得到一个缩进错误,但我似乎无法找到问题。我是python的新手,所以我敢肯定它显然很明显,但我看不到它。RaspberryPi Python IndentationError:预计有一个缩进块

#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 

请帮帮忙,我已经花了太长时间就这个问题和我似乎无法找到它的指压痕错误。

在此先感谢。

+1

根据[能够找到的所有在线示例](https://docs.python.org/2/tutorial/controlflow.html#defining-functions),整个函数体必须缩进。你究竟在哪里找到构造函数,函数体没有缩进? –

+0

你发布的错误回答你的问题... – coder

回答

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

之后的功能,你需要缩进你的代码。与shutdown功能相同。

+0

Thankyou,拯救了我的一天,我知道它必须是一件容易的事 –

+0

高兴地。 –

0

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

+0

谢谢你这么多,我知道这很简单。 –

0

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

相关问题