首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用函数变量调用类实例时,Python 'str‘对象没有'name’属性

使用函数变量调用类实例时,Python 'str‘对象没有'name’属性
EN

Stack Overflow用户
提问于 2016-01-31 19:13:29
回答 2查看 20.1K关注 0票数 3

我刚刚开始学习使用python编程。我正在尝试制作一个类似于精灵宝可梦的游戏,并且我正在尝试将OOP应用到游戏中。

我首先创建了一个精灵宝可梦类,并添加了类定义的属性,如name、HP、etype、attacks。“攻击”是一个列表字典。

我现在的问题是,我正在尝试开发一个简单的战斗引擎,我可以在其中切换口袋妖怪。出于这个原因,我创建了一个函数/方法名fight()。

在里面我有一个名为current_pokemon.name的变量,所以每当我切换口袋妖怪的时候,我都可以使用新的口袋妖怪的名字,攻击,等等。

我使用raw_input返回一个字符串来替换current_pokemon,它有一个.name属性可以在pikachu实例上调用。相反,我得到的'str‘对象没有属性。为什么这个不起作用?当我显式地编写pikachu.attribute时,它可以在fight()函数之外工作。

代码语言:javascript
复制
class Pokemon(object):
    def __init__(self, name, HP, etype, attacks):
        self.name = name
        self.HP = HP
        self.etype = etype
        self.attacks = attacks #2 attacks with name, dmg, type and power points

geodude = Pokemon("Geodude", 100, "Ground", 
                  attacks = 
                  {"Tackle":["Tackle",30,"Normal","35"], 
                   "Rollout":["Rollout",50,"Rock","20"]
                  })
                    #attacks = {attack:[attack_name, dmg, type, PP]}

pikachu = Pokemon("Pikachu", 100, "Lightning", 
                  attacks = 
                  {"Thunder Shock":["Thunder Shock",40,"Electric"],
                   "Quick Attack":["Quick Attack",40,"Normal"]
                  })

#selects pikachu's attack
print "Pokemon's name is", (pikachu.name)
print "Pikachu's %s attack damages %d" % ((pikachu.attacks["Thunder Shock"][0]),(pikachu.attacks["Thunder Shock"][1]))

pikachu.HP = pikachu.HP - geodude.attacks["Rollout"][1]
print "Pikachu's HP is", (pikachu.HP)
pikachu.HP = pikachu.HP - geodude.attacks["Tackle"][1]
print "Pikachu's HP is", (pikachu.HP)

#Pokemon Battle test with stored variable
#Value selector - replace all var attributes using an easy function
#Use Solution 2

def fight():
    current_pokemon = raw_input("pikachu or geodude? > ")
    print current_pokemon.name
    print current_pokemon.attacks


fight()

输出:

代码语言:javascript
复制
Pokemon's name is Pikachu
Pikachu's Thunder Shock attack damages 40
Pikachu's HP is 50
Pikachu's HP is 20
pikachu or geodude? > pikachu
Traceback (most recent call last):
  File "poke_game_stack.py", line 40, in <module>
    fight()
  File "poke_game_stack.py", line 36, in fight
    print current_pokemon.name
AttributeError: 'str' object has no attribute 'name'

请帮我解决这个简单的问题!谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-31 19:20:55

raw_input()总是返回一个字符串;它不会返回以相同名称存储的对象。

将pokemons存储在字典中(将字符串映射到对象),然后使用raw_input()中的字符串映射到其中一个对象:

代码语言:javascript
复制
pokemons = {
    'geodude': Pokemon("Geodude", 100, "Ground", 
                  attacks = 
                  {"Tackle":["Tackle",30,"Normal","35"], 
                   "Rollout":["Rollout",50,"Rock","20"]
                  }),

    'pikachu': Pokemon("Pikachu", 100, "Lightning", 
                  attacks = 
                  {"Thunder Shock":["Thunder Shock",40,"Electric"],
                   "Quick Attack":["Quick Attack",40,"Normal"]
                  }),
}

def fight():
    current_pokemon_name = raw_input("pikachu or geodude? > ")
    current_pokemon = pokemons[current_pokemon_name]
    print current_pokemon.name
    print current_pokemon.attacks
票数 5
EN

Stack Overflow用户

发布于 2016-01-31 19:15:15

代码语言:javascript
复制
current_pokemon = raw_input("pikachu or geodude? > ")
print current_pokemon.name
print current_pokemon.attacks

raw_input()方法返回一个str,字符串没有nameattacks等属性。

该函数从输入中读取行,将其转换为字符串(去掉尾随的换行符),并返回该字符串。当读取EOF时,将引发EOFError。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35113180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档