gpt4 book ai didi

python - Raspi 3 PIR 传感器 - Python 脚本 - 语法无效

转载 作者:行者123 更新时间:2023-12-01 04:01:11 27 4
gpt4 key购买 nike

实际上,我正在“魔镜”工作,现在我遇到了 python 脚本问题,应该打开/关闭我的显示器。

I copied the python script from this site

#!/usr/bin/env python

import sys
import time
import RPi.GPIO as io
import subprocess

io.setmode(io.BCM)
SHUTOFF_DELAY = 60 # seconds
PIR_PIN = 7 # Pin 26 on the board

def main():
io.setup(PIR_PIN, io.IN)
turned_off = False
last_motion_time = time.time()

while True:
if io.input(PIR_PIN):
last_motion_time = time.time()
sys.stdout.flush()
if turned_off:
turned_off = False
turn_on()
else:
if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY):
turned_off = True
turn_off()
time.sleep(.1)

def turn_on():
subprocess.call("sh /home/pi/Documents/PIR/monitor_on.sh", shell=True)

def turn_off():
subprocess.call("sh /home/pi/Documents/PIR/monitor_off.sh", shell=True)

if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
io.cleanup()

我尝试运行该脚本,但 python 告诉我第 25 行有语法错误,它正好指向 & 之后和 gt 之前的分号

我之前没有使用过python,所以我对python的语法一无所知。

如果你们能花一点时间帮助我解决我的问题,我将非常感激。

我得到了Python版本2.7.9

最佳答案

这不是原始 Python 文件的精确副本。您在复制文件时复制了一些 HTML 标记。

> 替换为 >

    if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY):
turned_off = True
turn_off()

您还存在缩进问题和其他应该删除的 HTML 内容:

def main():
io.setup(PIR_PIN, io.IN)
turned_off = False
last_motion_time = time.time()

def turn_on():
subprocess.call("sh /home/pi/Documents/PIR/monitor_on.sh", shell=True)

def turn_off():
subprocess.call("sh /home/pi/Documents/PIR/monitor_off.sh", shell=True)

关于python - Raspi 3 PIR 传感器 - Python 脚本 - 语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36470387/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com