簡體   English   中英

使用函數變量調用類實例時,Python的“ str”對象沒有屬性“名稱”

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

我剛剛開始學習使用python編程。 我正在嘗試制作類似於《口袋妖怪》的游戲,並且試圖在游戲中應用OOP。

首先,制作一個Pokemon類,並添加類定義的屬性,例如名稱,HP,etype,攻擊。 “攻擊”是列表的字典。

我現在的問題是,我正在嘗試開發一個簡單的戰斗引擎,在這里我可以換掉口袋妖怪。 因此,我將函數/方法命名為fight()。

在其中,我有一個名為current_pokemon.name的變量,因此每當切換出神奇寶貝時,我都可以使用新的神奇寶貝的名稱,攻擊等。

我使用了raw_input返回一個替換current_pokemon的字符串,該字符串具有一個在皮卡丘實例上調用的.name屬性。 相反,我得到的'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'

請幫助菜鳥這個簡單的問題! 謝謝!

raw_input()總是返回一個字符串; 它不會返回您使用相同名稱存儲的對象。

將您的神奇寶貝存儲在字典中(將字符串映射到對象),然后使用raw_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
current_pokemon = raw_input("pikachu or geodude? > ")
print current_pokemon.name
print current_pokemon.attacks

raw_input()方法返回一個str ,並且字符串沒有諸如nameattacks屬性。

該函數從輸入中讀取一行, 將其轉換為字符串 (帶末尾的換行符),然后將其返回。 讀取EOF時,將引發EOFError。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM