Python';str';对象没有属性';名称';使用函数变量调用类实例时

Python';str';对象没有属性';名称';使用函数变量调用类实例时,python,string,class,oop,Python,String,Class,Oop,我刚刚开始学习用python编程。我正在尝试制作一个类似于口袋妖怪的游戏,我正在尝试将OOP应用到agame中 我首先创建了一个Pokemon类,并添加了类定义的属性,如name、HP、etype和attacks。“攻击”是一个列表字典 我现在的问题是我正在尝试开发一个简单的战斗引擎,在那里我可以切换出口袋妖怪。因此,我创建了一个函数/方法名fight() 里面有一个名为current_pokemon.name的变量,所以每当我关掉pokemon时,我都可以使用新的pokemon的名称、攻击等

我刚刚开始学习用python编程。我正在尝试制作一个类似于口袋妖怪的游戏,我正在尝试将OOP应用到agame中

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

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

里面有一个名为current_pokemon.name的变量,所以每当我关掉pokemon时,我都可以使用新的pokemon的名称、攻击等

我使用raw_输入返回一个字符串来替换当前的_pokemon,它有一个.name属性来调用pikachu实例。相反,“str”对象没有属性。为什么这样不行?当我显式编写pikachu.attribute时,它在函数fight()之外工作

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()
输出:

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'
口袋妖怪的名字叫皮卡丘 皮卡丘的雷电冲击攻击造成40点伤害 皮卡丘的生命是50 皮卡丘的生命值是20 皮卡丘还是吉杜德?>皮卡丘 回溯(最近一次呼叫最后一次): 文件“poke_game_stack.py”,第40行,在 打架 文件“poke_game_stack.py”,第36行,在战斗中 打印当前\u pokemon.name AttributeError:“str”对象没有属性“name” 请帮我解决这个简单的问题! 谢谢

raw\u input()
方法返回一个
str
,字符串没有诸如
name
攻击等属性

函数从输入中读取一行,将其转换为字符串 (剥离尾随换行符),并返回该换行符。当读取EOF时, 提高了EOFError

raw\u input()
始终返回字符串;它不会返回以相同名称存储的对象

将口袋妖怪存储在字典中,(将字符串映射到对象),然后使用
raw\u input()
中的字符串映射到其中一个对象:

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

哇,从没想过!字典里的列表就是字典里的列表!我现在唯一的问题是,这被认为是不好的做法吗?做这种事情?你有结构化的对象;您的
Pokemon
实例封装了进一步的数据结构,为每种类型的Pokemon建模。这就是所有OO编程的工作原理。所以不,这不是一个坏习惯。谢谢你的简单而棒的解决方案!希望我能获得足够的知识来帮助像你一样的人:)好的,明白了。如果您不介意问,如果这些“单词”不是字符串,那么它们会调用class属性,您将它们称为什么?@iruvfriesoreos instance variables我已经考虑了很长时间了。谢谢穆罕默德!不客气@myleschuahiock,我希望这会有帮助!
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