AttributeError: 'str' object has no attribute
attributeerror, python
Solution
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())
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])'
If you REALLY needed to find the variable this way, you could use the `eval` function:
>>>foo = 'Hello World!'
>>>eval('foo')
'Hello World!'
or the `globals` function:
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')
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.
Problem
I'm pretty new to python programming and I wanted to try my hand at a simple text adventure game, but I've immediately stumbled on a roadblock. ``` 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() ``` That is my code, which for some reason throws that error: ``` 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' ``` I've search on the internet and in python documentation what the hell am I doing wrong here and I have no idea. If I'll put ID2 or ID3 in place of self.displayAll(letsago) it works perfectly, but it's pointless since player has no control over where he wants to go, so I'm guessing there is something wrong with trying to connect ID with the number from the dictionary, but I have no idea what to do and how to fix this.