Python AttributeError: 'str' 对象没有属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23913150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'str' object has no attribute
提问by user3683799
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.
我对 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()
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(fworthis)
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.
我在互联网和 python 文档中搜索过我到底做错了什么,我不知道。如果我将 ID2 或 ID3 代替 self.displayAll(letsago) 它可以完美地工作,但它毫无意义,因为玩家无法控制他想去的地方,所以我猜尝试连接 ID 有问题用字典中的数字,但我不知道该怎么做以及如何解决这个问题。
回答by BeetDemGuise
The problem is in your playerMovement
method. You are creating the string name of your room variables (ID1
, ID2
, ID3
):
问题出在你的playerMovement
方法上。您正在创建房间变量 ( 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
. 它不是变量。另外,我不认为它正在做你认为它在做的事情:
>>>str({'a':1}.values())
'dict_values([1])'
If you REALLYneeded to find the variable this way, you could use the eval
function:
如果您真的需要以这种方式找到变量,则可以使用该eval
函数:
>>>foo = 'Hello World!'
>>>eval('foo')
'Hello World!'
or the globals
function:
或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')
However, instead I would stronglyrecommend 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.
但是,相反,我强烈建议您重新考虑您的课程。您的userInterface
课程本质上是一个Room
. 它不应该处理玩家的移动。这应该在另一个类中,也许GameManager
或类似的东西。