【问题标题】:Sporadic Python error in while loop TypeError: 'int' object is not iterablewhile 循环 TypeError 中的零星 Python 错误:'int' 对象不可迭代
【发布时间】:2017-03-30 13:37:46
【问题描述】:

这是我的脚本有错误的部分:

while:
    sensorIn = 8
    sensorOut = 5
    sensorAC = 7
    blue = 0
    white = 1
    [tempIn, humIn] = grovepi.dht(sensorIn, 1)
    print("tempIn = %.02f C humIn =%.02f%%"%(tempIn, humIn))
    [tempOut, humOut] = grovepi.dht(sensorOut, 1)
    print("tempOut = %.02f C humOut =%.02f%%"%(tempOut, humOut))
    [tempAC, humAC] = grovepi.dht(sensorAC, 1)
    print("tempAC = %.02f C humAC =%.02f%%"%(tempAC, humAC))

它随机出错:

TypeError: 'int' object not iterable on the line: [tempAC, humAC] = grovepi.dht(sensorAC, 1)

这与 Raspberry Pi 和 GrovePi 板一起使用,以获取来自温度和湿度传感器的读数。它会循环播放,读取读数,然后对这些读数做一些事情,然后休眠 15 分钟,然后重新开始。

也许 while 循环是问题所在...我曾经在没有 while 循环的情况下运行它,并且会在 crontab 中将其设置为每 15 分钟运行一次。有时读数会出现错误,但会以 0 的形式通过脚本,脚本不会停止并出错,它会继续并仅将值报告为 0。

我查看了很多关于此错误的不同帖子,但我只遇到过总是出现错误并且根本不允许脚本运行完成的帖子,就我而言,它只是偶尔发生。

【问题讨论】:

    标签: python raspberry-pi grovepi+


    【解决方案1】:

    这似乎是 grovepi.dht 函数的一个已知问题,有时可能是由与设备的通信问题引起的。 http://forum.dexterindustries.com/t/incorrect-readings-and-typeerror/1582/2

    解决方案只是为类型错误添加异常处理,因为它只是一个间歇性问题(有关如何执行此操作的示例,请参阅链接的帖子)。

    【讨论】:

    • 感谢您的快速回复。我会尝试添加异常处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2013-10-31
    • 1970-01-01
    • 2018-08-17
    相关资源
    最近更新 更多