我遇到错误消息问题:TypeError: unsupported operand type(s) for -=: 'Workers' and 'int'。我知道该消息说这Workers不是int,但我不知道如何修复它。我的代码应该做什么:创建 5 个以生命力 100 开头的工人,我的工人名字就是当前的生命力。假设他们一次去工厂生产一单位的产品,如果他们成功了,他们就会失去 10 单位的生命力,并回到队列的最后,不得不在那里等待。但如果发生事故,他们会失去 100 个单位的生命力,死亡并从模拟中移除。只要还有工人活着,模拟就应该运行,并在最后打印出已经生产了多少单位的产品。如果我删除class Workers:第一个并将其更改def为class Roads::class Roads: def __init__(self): self._workers_road = [] for worker in range(5): self._workers_road.append(100)然后它工作得很好,但从class Workers:长远来看,我需要扩展我的代码并培育更多工人并将它们附加到class Roads:.import randomclass Workers: def __init__(self): self._worker = 100 def get_worker(self): return self._workerclass Products: def __init__(self): self._product = True def get_product(self): return self._productclass Roads: def __init__(self): self._workers_road = [] for worker in range(5): self._workers_road.append(Workers()) def workers_left(self): if len(self._workers_road) == 0: return Trueclass Storage: def __init__(self): self._product_stock = [] def add_product(self): self._product_stock.append(Products()) def get_product_stock(self): return 'Storage: ' + str(len(self._product_stock))class Plants: def __init__(self): self._plant_list = [] self._plant_storage = Storage() self._road = Roads() def add_worker(self): self._plant_list.append(self._road._workers_road[0]) self._road._workers_road.pop(0) def create_product(self): accident = random.randint(1,10) if accident < 5: self._plant_storage.add_product() self._plant_list[0] -= 10 else: self._plant_list[0] -= 100 def sub_worker(self): if self._plant_list[0] <= 0: self._plant_list.pop(0) else: self._road._workers_road.append(self._plant_list[0]) self._plant_list.pop(0)这似乎是一件相当容易解决的事情,但我被困住了......
添加回答
举报
0/150
提交
取消