Python 尝试制作口袋妖怪游戏时,List.remove()x不在列表中
虽然该行现在是冗余的,但是在运行一些迭代之后,应该从原始列表中删除self.y的行将引发“Valueerror:list.remove(x)x not in list”。我现在想不出我做错了什么Python 尝试制作口袋妖怪游戏时,List.remove()x不在列表中,python,Python,虽然该行现在是冗余的,但是在运行一些迭代之后,应该从原始列表中删除self.y的行将引发“Valueerror:list.remove(x)x not in list”。我现在想不出我做错了什么 from random import sample, choice class Selection(object): def __init__(self): self.pokemon_list = [ 'Blastoise', 'Charizar
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
不在列表中您是否多次调用bah.gary_pok()
?我是这样做的。哦,我知道了,谢谢。他们并没有在同一个列表上循环,他们正在从中删除。另外,不要发布代码的图片,将代码作为格式化文本发布与代码发布不一样的问题。您正在调用字符串上的删除。明白了,抱歉:-)