繁体   English   中英

List.remove()x尝试制作口袋妖怪游戏时不在列表中

[英]List.remove() x not in list while trying to make pokemon game

尽管该行现在是多余的,但是在运行几次迭代后,应该从原始列表中删除self.y的行引发了'Valueerror:list.remove(x)x不在列表中。 我无法弄清楚我现在在这里做错了什么。

from random import sample, choice


class Selection(object):


    def __init__(self):

        self.pokemon_list = [
            'Blastoise', 'Charizard', 'Venasaur',   
            'Artiquno', 'Moltress', 'Zapdos', 
        ]

        self.x = sample(self.pokemon_list, 3)

        print """
    CONGRATULATIONS TRAINER, YOU HAVE BEEN GIVEN %s,
    YOUR JOURNEY BEGINS NOW! PROCEED WISELY OR DIE.
    """ % (self.x)


    def gary_pok(self):

        for self.z in self.x:

            if self.z == "Blastoise":
                self.y = 'Venasaur'
                **self.pokemon_list.remove(self.y)
                print self.pokemon_list**
            elif self.z == "Charizard":
                self.y = 'Blastoise'
                print self.pokemon_list.remove(self.y)
            elif self.z == "Venasaur":
                self.y = 'Charizard'
                print self.pokemon_list.remove(self.y)
            else:
                self.y = choice(self.pokemon_list)
                print self.pokemon_list.remove(self.y)

        print "Gary's pokemons:{}, Nidoqueen and Archanine.".format(self.y)

bah = Selection()
bah.gary_pok()      

遍历时,无法从列表中删除项目。 因为,当第一次发生循环时,当它达到您的条件时,它将从您的列表中删除项目;当循环进行第二次迭代时,它希望删除已经被删除的项目。

在此处输入图片说明

在打印Artiquioni之后,将Charizard移除,然后在下一次迭代中, Charizard不在列表中

暂无
暂无

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

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