简体   繁体   English

Python说在赋值之前提到的变量在嵌套在for循环中时被赋值吗?

[英]Python saying variable mentioned before assignment when it is assigned in the for loop it's nested in?

EDIT 编辑
Well I'm an idiot for deleting the thing I'm looping through. 好吧,我是删除我正在循环通过的东西的白痴。 Thanks y'all. 谢谢你们


I've tried looking up answers but honestly this is just boggling me. 我尝试查找答案,但老实说这只是让我感到困惑。
I have a for loop which loops through lists in a list, but then in an if statement nested into the for loop it says the thing it is looping with is not defined? 我有一个for循环,它循环遍历列表中的列表,但是在嵌套在for循环中的if语句中,它说与之循环的对象未定义吗?

for monster in self.monsters:
    print("google chrome")
    if monster[1][1] <= 0:
        self.exp_gotten += monster[1][8] 
        for j in self.turns:
            if j[0] == monster[1][0]:
                del j
                self.defeated_monsters.append(monster)
                del monster

self.monsters is a list containing other lists for the monsters. self.monsters是一个包含其他怪物列表的列表。 The lists are like ["Monster Name", [monster_id, hp, mp, ...]]. 列表类似于[“ Monster Name”,[monster_id,hp,mp,...]]。 Monster[1][1] is the HP stat, and it checks if it is smaller than 0, indicating its been defeated. Monster [1] [1]是HP的状态,它会检查它是否小于0,表明它已被击败。 It then adds the exp, then deletes the monster from the turns list, adds it to the list of defeated monsters, then deletes the monster itself. 然后添加经验值,然后从转弯列表中删除怪物,将其添加到失败的怪物列表中,然后删除怪物本身。
Except when it comes to this part, I keep getting "monster variable referenced before assignment" 除非涉及到这部分,否则我会不断收到“分配前引用的怪物变量”

File "C:\Users\Admin\Documents\Dave's stuff\Python\COURSEWORK\battle.py", line 150, in check_if_fin
    if j[0] == monster[1][0]:
UnboundLocalError: local variable 'monster' referenced before assignment

I just don't get what I'm doing wrong. 我只是不明白我在做什么错。

Your inner for loop deletes the monster variable: 您的内部for循环会删除monster变量:

    for j in self.turns:
        if j[0] == monster[1][0]:
            del j
            self.defeated_monsters.append(monster)
            del monster
            ^^^^^^^^^^^

If self.turns contains more than one element and and anything but the last enters the if block, you'll delete monster and get that error. 如果self.turns包含多个元素以及除最后一个元素之外的其他任何内容, if您输入if块, if删除monster并得到该错误。 You probably want to throw in a break statement or otherwise exit the loop if you're done. 您可能想要插入break语句,或者如果完成则退出循环。

It's tricky to remove objects from a list that you're currently iterating over. 从当前正在迭代的列表中删除对象非常棘手。 One solution would be to use intermediate lists: 一种解决方案是使用中间列表:

new_monsters = []

for monster in self.monsters:
    # It's usually clearer to remove unnecessarily nested `if` statements
    if monster[1][1] > 0:
        continue

    self.exp_gotten += monster[1][8] 

    for j in self.turns:
        if j[0] == monster[1][0]:
            self.defeated_monsters.append(monster)

            break  # Don't continue checking, you've already defeated the monster
    else:
        # We never broke out of the above loop, so we never defeated the monster
        new_monsters.append(monster)

self.monsters = new_monsters

Finally, code like monster[1][8] and monster[1][1] is really difficult to manage. 最后,像monster[1][8]monster[1][1]的代码确实很难管理。 Consider setting up your monster as a class with descriptive attributes or at the very least a namedtuple . 考虑将您的怪物设置为具有描述性属性的类,或者至少设置为namedtuple That way, you can use monster.exp instead of monster[1][8] . 这样,您可以使用monster.exp代替monster[1][8]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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