【问题标题】:Can't solve 'NoneType' object is not iterable TypeError无法解决“NoneType”对象不可迭代 TypeError
【发布时间】:2017-03-26 00:38:48
【问题描述】:

链接: main.py:http://pastebin.com/19qBqWcF

类文件夹:

game.py: http://pastebin.com/P7Degwna (我没有足够的声誉发布两个以上的链接。以下断开的链接是为了完整性,但我相信它们的代码完全没问题)

magic.py: http://pastebin.com/wpwSCDe7

inventory.py:http://pastebin.com/8kFXJne1

我正在努力成为一名更好的程序员,并且一直在学习一个简单战斗系统的教程。目前正在执行敌人的战斗战术。

我正在尝试编程,如果敌人选择治疗并且剩余 50% 以上,他们会重新选择一个法术。 每当发生这种情况时,我的代码都会出现以下错误

Traceback (most recent call last):
  File "C:/Users/Kyle/PycharmProjects/battle/main.py", line 160, in <module>
    choose_attack, spell, magic_dmg = enemy.choose_enemy_spell()
TypeError: 'NoneType' object is not iterable

这是之前输出到终端的转储:

ACTIONS:
    1. Attack
    2. Magic
    3. Items
Choose action:1

TARGET:
    1. Ganon
    2. Vaati
Choose target:1
player attacked enemy for 284 points of damage.


Begin method
Curaga INSIDE CLASS
This is happening.
Begin method
Meteor INSIDE CLASS
1186 INSIDE CLASS INSIDE METHOD INSIDE FINAL ELSE

我可以看到我的玩家轮到他们,然后敌人选择了治疗法术,HP 太多所以选择了另一个法术,然后我得到了 noneType 错误。

每当第一次选择另一个不是 curaga 的咒语时,代码就可以正常工作。

相关代码部分:

这是我的主程序

#Create Black Magic
fire = Spell("Fire", 25, 600, "black") #create object fire from class Spell
thunder = Spell("Thunder", 25, 600, "black")
blizzard = Spell("Blizzard", 25, 600, "black")
meteor = Spell("Meteor", 40, 1200, "black")
quake = Spell("Quake", 32, 900, "black")

#Create White Magic
cure = Spell("Cure", 25, 620, "white")
cura = Spell("cura", 32, 1500, "white")
curaga = Spell("Curaga", 50, 6000, "white")


#Create lists of magic and items associated with players
player_magic = [fire, thunder, blizzard, meteor, cure, cura] #Create list of magic spell objects for object player
player_items = [{"item": potion, "quantity": 15}, {"item": hipotion, "quantity": 5}, {"item": superpotion, "quantity": 5},
            {"item": elixer, "quantity": 5}, {"item": hielixer, "quantity": 2}, {"item": grenade, "quantity": 5},] #Create list of item objects for object player
#Creat lists of magic and items associated with enemies
enemy_spells = [fire, meteor, curaga]
eneny_items = []

#Istantiate Player objects
player1 = Person("Zelda", 3260, 132, 300, 34, player_magic, player_items)
#Inistantiating object player using class Person
player2 = Person("Link ", 4160, 188, 311, 34, player_magic, player_items)
player3 = Person("Sheik", 3089, 174, 288, 34, player_magic, player_items)
#Instantiate Enemy objects
enemy1 = Person("Vaati", 1250, 130, 560, 325, enemy_spells, [])
enemy2 = Person("Ganon", 18200, 701, 535, 25, enemy_spells, [])
#Instantiating object enemy using class Person
enemy3 = Person("Vaati", 1250, 130, 560, 325, enemy_spells, [])

# Enemy chose to use magic
if enemy_choice == 1:
    choose_attack, spell, magic_dmg = enemy.choose_enemy_spell()
    enemy.reduce_mp(spell.cost)
    if spell.type == "white":  # Check type of spell of object spell to determine how to handle the chosen spell.
        enemy.heal(magic_dmg)  # White typer means heal
        print(bcolours.OKBLUE + "\n" + spell.name, " heals", enemy.name + str(magic_dmg),
              "HP." + bcolours.ENDC)
    elif spell.type == "black":  # black type means do damage
        target = random.randrange(0, 3)
        players[target].take_damage(magic_dmg)
        print(bcolours.OKBLUE + "\n" + enemy.name.replace(" ", "") + "'s " + spell.name, "deals",
              str(magic_dmg), "points of damage to " + players[target].name + bcolours.ENDC)
        if players[target].get_hp() == 0:
            print(players[target].name + " has died.")
            del players[target]

这是来自一个包含类的文件

#Determine the spell the enemy chooses to use
def choose_enemy_spell(self):

    print("Begin method")
    choose_attack = 0
    magic_choice = random.randrange(0, len(self.magic))
    spell = self.magic[magic_choice]
    magic_dmg = spell.generate_damage()
    print(spell.name, "INSIDE CLASS")
    pct = (self.hp / self.maxhp)*100 #percentage of health remaining
    if self.mp < 25:
        print("This probably isnt happening")
        choose_attack = 1
        return choose_attack, spell, magic_dmg
    if  (spell.type == "white" and pct > 50) or self.mp < spell.cost:
        print("This is happening.")
        self.choose_enemy_spell()
    else:
        print(magic_dmg, "INSIDE CLASS INSIDE METHOD INSIDE FINAL ELSE")
        return choose_attack, spell, magic_dmg

【问题讨论】:

    标签: python nonetype


    【解决方案1】:

    http://pastebin.com/P7Degwna 的第 172 行缺少一个 return

    变化:

    self.choose_enemy_spell()
    

    收件人:

    return self.choose_enemy_spell()
    

    调试思路如下:

    • 先看回溯消息,Traceback (most recent call last): File "C:/Users/Kyle/PycharmProjects/battle/main.py", line 160, in choose_attack, spell, magic_dmg = enemy.choose_enemy_spell() TypeError: 'NoneType' object is not iterable

    • 它告诉你的是 enemy.choose_enemy_spell() 返回 None 而不是 3 元组。

    • 接下来,查看Person.choose_enemy_spell 中的代码,检查所有的退出路径,看看哪个有问题。

    • 第 169 行和第 175 行的返回都正确返回了一个三元组 return choose_attack, spell, magic_dmg。但是,第 172 行的代码计算了预期的结果而不返回它。这就是为什么您只在 (spell.type == "white" and pct &gt; 50) or self.mp &lt; spell.cost 评估为 true 时看到错误的原因。

    否则,您的代码看起来不错。希望这个小小的调试练习能够满足您的技能培养目标:-)

    【讨论】:

    • 非常感谢您的回复!看起来像一个简单的错误,但它确实清除了脑海中的过程!在我的脑海中,我将那行代码移回方法的开头并期望它这样做,直到它到达 else 语句现在我实际上正在考虑它开始方法,移动到 elf 语句再次开始方法并到达 else 语句并将其返回给 elif 语句,然后该语句没有返回任何预期的代码主体!谢谢!
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多