簡體   English   中英

RaspberryPi Python IndentationError:應為縮進塊

[英]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之前縮進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM