简体   繁体   中英

Python 'str' object has no attribute 'name' when using a function variable to call a class instance

I'm just starting to learn programming with python. I'm trying to make a game similar to Pokemon, and I'm trying to apply OOP into agame.

I started by making a Pokemon class, and adding class-defined attributes such as name, HP, etype, attacks. "attacks" is a dictionary of lists.

My problem now is I'm trying to develop a simple battle engine where I can switch out pokemons. For this reason, I made a function/method name fight().

Inside that I have a variable named current_pokemon.name so whenever I switch out the pokemon, I can use the new pokemon's name, attacks, etc.

I used raw_input to return a string that replaces current_pokemon, which has a .name attribute to call on a pikachu instance. Instead, I get the 'str' object has no attribute. Why doesn't this work? It worked outside the function fight() when I explicitly wrote pikachu.attribute.

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()

Output:

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'

Please help noob me with this simple problem! Thanks!

raw_input() always returns a string; it will not return an object you stored by the same name.

Store your pokemons in a dictionary, (mapping string to object) then use the string from raw_input() to map to one of those objects:

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
current_pokemon = raw_input("pikachu or geodude? > ")
print current_pokemon.name
print current_pokemon.attacks

The raw_input() method returns a str , and strings have no property such as name or attacks .

The function reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM