如何通过使用字符串引用列表中对象的属性?

我正在编写一个文字冒险游戏,我试图输入一个房间中的对象的输入,在对象列表中搜索它,然后获取该对象并将其附加到清单(inv)列表中。我需要使用其名称的输入(即属性之一)来搜索对象。

class room():
    def __init__(self, name):
        self.objects = []

class player(room):
    def __init__(self, name, inv):
        self.name = name
        self.inv = []

class things(room):
    def __init__(self, name, is_weapon):
        self.name = name
        self.weapon = is_weapon



currentRoom = center
objLen = len(currentRoom.objects)
if currentRoom.objects:
    for x in range(len(currentRoom.objects)):
        print("Objects here: ",currentRoom.objects[x].name)
    pickUp = input("Would you like to take any objects: ")

    for a in range(0,objLen):
        if pickUp.upper() == currentRoom.objects.name:
            ind = currentRoom.objects.index(pickUp.upper().name)
            Andy.inv.append(currentRoom.objects[ind])
            currentRoom.objects.pop[ind]
        else:
            print("Object not found in this room!")
评论