gpt4 book ai didi

python3 : how to use for loop and if statements over class attributes?

转载 作者:行者123 更新时间:2023-12-02 06:37:30 27 4
gpt4 key购买 nike

我有以下类(class):

class equipment:
def __init__(self, name, location, status):
self.name = name
self.location = location
self.status=status

class device(equipment):
def __init__(self, name, location,status):
super().__init__(name, location,status)


device1 = device("device1 ", (5, 2), 0)
device2 = device("device2", (10, 2), 0)
device3 = device("device3", (15, 3), 0)
device4 = device("device4", (5, 5), 0)

all_devices = ['device1', 'device2', 'device3', 'device4']

并基于另一个代码:我得到 status_devices=[1,0,1,1] 所以我想通过使用 for 循环和 if 语句来更新原始状态(仅打印 status=1 的设备),

for n in range(0, 4):
all_devices[n].status=status_devices[n]

这种方式给了我错误:str对象没有属性状态

谢谢

最佳答案

导致您提到的错误的问题是您有

all_devices = ['device1', 'device2', 'device3', 'device4']

而不是

all_devices = [device1, device2, device3, device4]

(也就是说,您创建了一个字符串列表,而不是一个设备对象列表。)

关于python3 : how to use for loop and if statements over class attributes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59672780/

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