将字符串转换为已声明变量的变量名



我正在尝试将字符串转换为变量名。我已经声明了这个变量,并想用它来声明另一个变量。例如,我有一个名为player的类,想用这个代码更改玩家所穿的盔甲。

gt = gettext("playerarmor", 1, 1)
for i in armorlist:
    if i == gt:
        player.armor = i

但是这里的gettext函数:

def gettext(header, move, count):
    with open('dictionary.txt', 'r') as f:
        f = f.read()
        lines = f.split("n")
    for i, line in enumerate(lines):
        if header in line:
            word = []
            for l in range(0, count):
                newindex = i + move
                word.append(lines[newindex+l])
            return "n".join(word)

仅返回一个已声明的CCD_ 3字符串。我怎样才能做到这一点?

字符串不能成为变量名

我建议用字符串作为元素,用值作为值的字典

最新更新