AttributeError: 'str' 对象没有属性 [英] AttributeError: 'str' object has no attribute
问题描述
我对 Python 编程还很陌生,我想尝试一个简单的文本冒险游戏,但我立即发现了一个障碍.
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'
我在互联网和 Python 文档中搜索过我到底做错了什么,但我不知道.如果我将 ID2 或 ID3 代替 self.displayAll(letsago) 它可以完美运行,但它毫无意义,因为玩家无法控制他想去的地方,所以我猜尝试连接 ID 有问题用字典中的数字,但我不知道该怎么做以及如何解决这个问题.
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.
推荐答案
问题出在您的 playerMovement
方法中.您正在创建房间变量的字符串名称(ID1
、ID2
、ID3
):
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.
这篇关于AttributeError: 'str' 对象没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!