python - while 循环不会循环或执行

标签 python python-3.x while-loop

我正在使用一个系统,该系统应该使我的树莓派 GPIO 引脚之一处于高电平大约 2 秒。我把它分成两个不同的文件。 “website”文件(名为 app.py)和“GPIO”文件(名为 test.ty)。测试文件的请求方式如下:

from flask import Flask, render_template
from test import open_door

app = Flask(__name__)

@app.route('/opendoor')
def openDoor():
    open_door()


if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

test.py 文件如下所示:

import RPi.GPIO as GPIO
import time

testPin = 18

GPIO.setmode(GPIO.BCM)
GPIO.setup(testPin, GPIO.OUT)

counter =0

def open_door():
    try:
        print ("Everthing is fine")
        while counter < 900000:
             print ("Everything is good")
             GPIO.output(testPin, GPIO.HIGH)

             counter += 1

    except:
        print ("Everything is oke!")

    finally:
        GPIO.cleanup()

我收到消息“一切都很好”和“一切都很好!”但不是“一切都很好”的信息。在我看来, while 循环没有执行。 有人知道为什么它不起作用吗?

最佳答案

counter 不在调用 open_door() 的其他文件的范围内,这就是为什么您会看到“一切正常!”因为未知变量是一个异常(exception)

关于python - while 循环不会循环或执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47841056/

相关文章:

multithreading - bash while 循环线程

php - 按 php 中类似的 2 个字段分组并得到总计数结果?

python - Python中的生日悖论与蒙特卡罗方法?

python - IOError: [Errno 22] 无效模式 ('wb' ) 或文件名:

Python 网络抓取。 xpath 返回一个空数组

python - python中的变量范围和Try Catch

python - 获取 python3 中特定字符串部分的数据类型

python - 命名元组实例的酸洗正常成功,但在模块被 Cythonized 时失败

mysql - 在 WHILE 循环中 INSERT 未插入数据库

python - 如何在 Django 中按日期对对象进行分组?