sedond Pass上的Python For循环错误

2024-05-16 01:36:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用for循环,如下所示:

addresses = [0x30, 0x31, 0x32, 0x33]

for address in addresses:
   print(address)
   chirp = chirp.Chirp(address=address,
                       read_moist=True,
                       read_temp=True,
                       read_light=True,
                       min_moist=False,
                       max_moist=False,
                       temp_scale='farenheit',
                       temp_offset=0)
   chirp.trigger()
   log_values(address, chirp.moist, chirp.temp, chirp.light)
   time.sleep(1)

它似乎在控制台的第一个过程中工作,我看到48,然后49,然后得到这个错误:

Traceback (most recent call last):
File "readings_logger.py", line 17, in <module>
chirp = chirp.Chirp(address=address,
AttributeError: 'Chirp' object has no attribute 'Chirp'

我想这说明它跑过一次。然后在第二个关口就跑不动了。我的第一个想法是局部变量的问题,所以尝试删除'chirp'小写'c',但没有帮助。我猜这是线程,但我是新的python和不知道如何防止这一点


Tags: infalsetrueforreadaddressaddressesmin
1条回答
网友
1楼 · 发布于 2024-05-16 01:36:52

当您将chirp设置为chirp.Chirp(...)时,您正在重新分配chirp

尝试将变量赋值(在print(address)之后)更改为类似于loop_chirp = chirp.Chirp(...)。随后的每一行现在都将更改为loop_chirp,后跟属性

相关问题 更多 >