为什么我会收到“IndentationError:期望缩进块”?

时间:2017-11-14 01:48:27

标签: python-2.7 raspberry-pi3

#!/usr/bin/python



# Import der Python libraries

import RPi.GPIO as GPIO

import time

import datetime

import subprocess



# Verwendung des Board Mode, Angabe der PIN Nummern anstelle der GPIO BCM Nummer

GPIO.setmode(GPIO.BOARD)



# GPIO definieren, bei mir ist es PIN 8

GPIO_PIR = 8



#  GPIO als "Input" festlegen

GPIO.setup(GPIO_PIR,GPIO.IN)



Current_State  = 0

Previous_State = 0



try:



# erst mal schlafen bis der bootvorgang abgeschlossen ist

time.sleep(60)



# Warten bis Sensor sich meldet

while GPIO.input(GPIO_PIR)==1:

  Current_State  = 0



subprocess.Popen('echo initilized PIR | wall', shell=True)



# Schleife bis CTRL+C

while True :



  #Status von Sensor auslesen

  Current_State = GPIO.input(GPIO_PIR)



  if Current_State==1 and Previous_State==0:



    # Kommando zum anschalten, Frambuffer erneuern

    subprocess.Popen('echo Monitor on | wall', shell=True)

    subprocess.Popen('/opt/vc/bin/tvservice -p', shell=True)

    subprocess.Popen('fbset -depth 8', shell=True)

    subprocess.Popen('fbset -depth 16', shell=True)

    subprocess.Popen('sudo /bin/chvt 6 && sudo /bin/chvt 7', shell=True)

    Previous_State=1



  elif Current_State==0 and Previous_State==1:



    # Ausschalten des Monitors

    subprocess.Popen('echo Monitor off | wall', shell=True)

    subprocess.Popen('/opt/vc/bin/tvservice -o', shell=True)

    Previous_State=0



  # 5 Sek Warten

  time.sleep(5)



except KeyboardInterrupt:

print " Exit"

GPIO.cleanup()

您好, 我得到了:

File "PIR.py", line 30
  time.sleep(60)
     ^
IndentationError: expected an indented block
你能帮帮我吗? 当我在" time.sleep"之前设置标签时它在行#34中出现语法错误,而GPIO.input(GPIO_PIR)== 1:"。 我使用这个脚本管理魔镜的PIR传感器,但它无法工作。所以我在终端写了" python PIR.py"。之后,我认识到错误,PIR传感器无法工作的逻辑。但我是python中的菜鸟,所以我想得到你的帮助。

1 个答案:

答案 0 :(得分:0)

块是程序或脚本中的一组语句。通常它由至少一个语句和块的声明组成,具体取决于编程或脚本语言。允许使用块进行分组的语言称为块结构化语言。 Python就是这样一种语言。有很多文档和教程可以告诉你原因。这是一个开始:https://www.python-course.eu/python3_blocks.php

当您需要使用它们时,您的代码会错过其中一些块结构,例如try:else:。您看到的其他示例是while True:和if - else语句。

#!/usr/bin/python
# Import der Python libraries
import RPi.GPIO as GPIO
import time
import datetime
import subprocess

# Verwendung des Board Mode, Angabe der PIN Nummern anstelle der GPIO BCM Nummer
GPIO.setmode(GPIO.BOARD)
# GPIO definieren, bei mir ist es PIN 8
GPIO_PIR = 8
#  GPIO als "Input" festlegen
GPIO.setup(GPIO_PIR,GPIO.IN)
Current_State  = 0
Previous_State = 0
try:
  # erst mal schlafen bis der bootvorgang abgeschlossen ist
  time.sleep(60)
  # Warten bis Sensor sich meldet
  while GPIO.input(GPIO_PIR)==1:
    Current_State  = 0
  subprocess.Popen('echo initilized PIR | wall', shell=True)
  # Schleife bis CTRL+C
  while True :
    #Status von Sensor auslesen
    Current_State = GPIO.input(GPIO_PIR)
    if Current_State==1 and Previous_State==0:
      # Kommando zum anschalten, Frambuffer erneuern
      subprocess.Popen('echo Monitor on | wall', shell=True)
      subprocess.Popen('/opt/vc/bin/tvservice -p', shell=True)
      subprocess.Popen('fbset -depth 8', shell=True)
      subprocess.Popen('fbset -depth 16', shell=True)
      subprocess.Popen('sudo /bin/chvt 6 && sudo /bin/chvt 7', shell=True)
      Previous_State=1
    elif Current_State==0 and Previous_State==1:
      # Ausschalten des Monitors
      subprocess.Popen('echo Monitor off | wall', shell=True)
      subprocess.Popen('/opt/vc/bin/tvservice -o', shell=True)
      Previous_State=0
    # 5 Sek Warten
    time.sleep(5)
except KeyboardInterrupt:
  print " Exit"
  GPIO.cleanup()