且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AttributeError: 'str' 对象没有属性

更新时间:2023-12-02 23:09:52

问题出在您的 playerMovement 方法中.您正在创建房间变量的字符串名称(ID1ID2ID3):

The problem is in your playerMovement method. You are creating the string name of your room variables (ID1, ID2, ID3):

letsago = "ID" + str(self.dirDesc.values())

然而,你创建的只是一个str.它不是变量.另外,我不认为它正在做你认为它在做的事情:

However, what you create is just a str. It is not the variable. Plus, I do not think it is doing what you think its doing:

>>>str({'a':1}.values())
'dict_values([1])'

如果您真的需要以这种方式找到变量,您可以使用eval函数:

If you REALLY needed to find the variable this way, you could use the eval function:

>>>foo = 'Hello World!'
>>>eval('foo')
'Hello World!'

globals 函数:

class Foo(object):
    def __init__(self):
        super(Foo, self).__init__()
    def test(self, name):
        print(globals()[name])

foo = Foo()
bar = 'Hello World!'
foo.text('bar')

但是,相反,我强烈建议您重新考虑您的课程.您的 userInterface 类本质上是一个 Room.它不应该处理玩家的移动.这应该在另一个类中,可能是 GameManager 或类似的东西.

However, instead I would strongly recommend you rethink you class(es). Your userInterface class is essentially a Room. It shouldn't handle player movement. This should be within another class, maybe GameManager or something like that.