Python 属性错误:';str';对象没有属性

Python 属性错误:';str';对象没有属性,python,attributeerror,Python,Attributeerror,我对python编程相当陌生,我想尝试一个简单的文本冒险游戏,但我立即遇到了一个障碍 class userInterface: def __init__(self, roomID, roomDesc, dirDesc, itemDesc): self.roomID = roomID self.roomDesc = roomDesc self.dirDesc = dirDesc self.itemDesc = itemDesc

我对python编程相当陌生,我想尝试一个简单的文本冒险游戏,但我立即遇到了一个障碍

class userInterface:
    def __init__(self, roomID, roomDesc, dirDesc, itemDesc):
        self.roomID = roomID
        self.roomDesc = roomDesc
        self.dirDesc = dirDesc
        self.itemDesc = itemDesc

    def displayRoom(self): #Displays the room description
        print(self.roomDesc)

    def displayDir(self): #Displays available directions
        L1 = self.dirDesc.keys()
        L2 = ""
        for i in L1:
                L2 += str(i) + " "
        print("You can go: " + L2)

    def displayItems(self): #Displays any items of interest
        print("Interesting items: " + str(self.itemDesc))

    def displayAll(self, num): #Displays all of the above
        num.displayRoom()
        num.displayDir()
        num.displayItems()

    def playerMovement(self): #Allows the player to change rooms based on the cardinal directions
        if input( "--> " ) in self.dirDesc.keys():
            letsago = "ID" + str(self.dirDesc.values())
            self.displayAll(letsago)
        else:
            print("Sorry, you can't go there mate.")



ID1 = userInterface(1, "This is a very small and empty room.", {"N": 2}, "There is nothing here.")

ID2 = userInterface(2, "This is another room.", {"W": 3}, ["knife", "butter"])

ID3 = userInterface(3, "This is the third room. GET OVER HERE", {}, ["rocket launcher"])

ID1.displayAll(ID1)
ID1.playerMovement()
这是我的代码,出于某种原因会抛出该错误:

Traceback (most recent call last):
  File "D:/Python34/Text Adventure/framework.py", line 42, in <module>
    ID1.playerMovement()
  File "D:/Python34/Text Adventure/framework.py", line 30, in playerMovement
    self.displayAll(fuckthis)
  File "D:/Python34/Text Adventure/framework.py", line 23, in displayAll
    num.displayRoom()
AttributeError: 'str' object has no attribute 'displayRoom'
回溯(最近一次呼叫最后一次):
文件“D:/Python34/Text Adventure/framework.py”,第42行,在
ID1.玩家移动()
playerMovement中第30行的文件“D:/Python34/Text Adventure/framework.py”
self.displayAll(操这个)
displayAll中第23行的文件“D:/Python34/Text Adventure/framework.py”
显示室数目()
AttributeError:“str”对象没有属性“displayRoom”

我在互联网上和python文档中搜索过我到底做错了什么,我不知道。如果我用ID2或ID3代替self.displayAll(letsago),它工作得很好,但毫无意义,因为玩家无法控制他想去哪里,所以我猜尝试将ID与字典中的数字连接是有问题的,但是我不知道该怎么做以及如何解决这个问题。

问题在于您的
playermovation
方法。您正在创建房间变量的字符串名称(
ID1
ID2
ID3
):

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

>>>str({'a':1}.values())
'dict_values([1])'
如果确实需要通过这种方式查找变量,可以使用
eval
功能:

>>>foo = 'Hello World!'
>>>eval('foo')
'Hello World!'
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')
globals
功能:

>>>foo = 'Hello World!'
>>>eval('foo')
'Hello World!'
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')

然而,我强烈建议你重新思考你的课程。您的
用户界面
类本质上是一个
房间
。它不应该处理玩家的移动。这应该在另一个类中,可能是
GameManager
或类似的类。

letsago
是一个字符串,而不是
userInterface
的实例。当您将文件室的ID发送到
.displayAll()
中时,请确保a)在列表中查找文件室并获取该文件室(userInterface的实例)然后调用
.displayAll()
或将userinterface的实例直接传递到
displayAll()
中。一旦代码正常工作,请随时发布,因为我有一些注释不适用于此设置:D