为什么Python看不到指定的int()属性?

2024-09-28 05:17:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个基于Python的、单人的、基于单词的MMORPG游戏。我是一个初学者,希望这是一个简单的任务。我对移动部分进行了编码,其中角色从一个站点移动到另一个站点。它似乎不起作用,因为Python似乎无法看到我的属性。这是错误消息:

Traceback (most recent call last): File "C:/Users/lenovo/Desktop/Maelstrom/Maelstrom Move.py", line 51, in place = test.place AttributeError: 'Player' object has no attribute 'place'

这是我的密码:

class Player(object):
    """The player."""
    def __init__(self,name="name",inv=[],equip=[],stats=[],place=int("001"),knownplaces={}):
        self.name = input("What name do you want?")
        knownplaces[int("001")]="Ruby City"
        knownplaces[int("002")]="Ruby Inn"
        knownplaces[int("003")]="Ruby Forests"
        knownplaces[int("004")]="Ruby Countryside"
        knownplaces[int("005")]="Witch Hideout"
    def __str__():
        rep = self.movepossible
    def movepossible(self,position):
        #001--Ruby City
        #002--Ruby Inn
        #003--Ruby Forests
        #004--Ruby Countryside
        #005--Witch Hideout
        if position==int("001"):
            possible=[int("002"),int("003")]
            return possible
        elif position==int("002"):
            possible=[int("001")]
            return possible
        elif position==int("003"):
            possible=[int("001"),int("004")]
            return possible
        elif position==int("004"):
            possible=[int("001"),int("003"),int("005")]
            return possible
        elif position==int("005"):
            possible=[int("004")]
            return possible
        else:
            return null
    def move(self,position):
        possiblewords=[]
        print('Choose between paths:'/n)
        possible = movepossible(self, position)
        for m in range(0,len(possible),1):
            possiblewords.append(knownplaces[possible[m]])
        for n in range(0,len(possiblewords),1):
            print(m+':'+possiblewords[m-1] /n)
        choice=input('Make your choice...')
        if choice-1 <= len(possiblewords):
            self.place=possible[choice-1]

    def showposition(self):
        print(knownplaces[self.place])
test = Player()
while True:
    place = test.place
    test.move(place)
    test.showposition()

Tags: nameintestselfreturndefpositionplace
1条回答
网友
1楼 · 发布于 2024-09-28 05:17:24

在执行行place = test.place时,尚未定义Player实例上的place属性

第一次设置place属性是在move()方法中。i、 e.在调用move()之前尝试访问place将导致您观察到的错误

应该为Player类的初始值设定项中的self.place指定一个默认值

相关问题 更多 >

    热门问题