如何在Python中使用for循环为多个实例调用相同的class属性?

如何在Python中使用for循环为多个实例调用相同的class属性?,python,for-loop,Python,For Loop,我试图使用for循环检查用户是否在我的pygame中单击了一个库存插槽。我已经创建了名为“slot1”、“slot2”、“slot3”等的插槽,一直到“slot13”。我的问题是,;如何在每次迭代的“slot”后面添加数字 if event.type == pg.MOUSEBUTTONDOWN and event.button == 3: for i in range(14): if mouse_click and inventory_state and slot+str

我试图使用for循环检查用户是否在我的pygame中单击了一个库存插槽。我已经创建了名为“slot1”、“slot2”、“slot3”等的插槽,一直到“slot13”。我的问题是,;如何在每次迭代的“slot”后面添加数字

if event.type == pg.MOUSEBUTTONDOWN and event.button == 3:
    for i in range(14):
        if mouse_click and inventory_state and slot+str(i).collidepoint(pos):
            inventory.equip_item(inventory.items[i])

欢迎来到Tripti社区。对于高达13的范围,您可以将i的值存储在变量中,每次都附加“slot”。我想那会解决你的问题的

不要将插槽存储在不同的变量中,而是存储在列表中。然后您可以迭代此列表:

for i in range(13):
    if mouse_click and inventory_state and slots[i].collidepoint(pos):
        inventory.equip_item(inventory.items[i])