Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:list。移除(x):x不在列表中,尽管条件检查已通过_Python - Fatal编程技术网

Python ValueError:list。移除(x):x不在列表中,尽管条件检查已通过

Python ValueError:list。移除(x):x不在列表中,尽管条件检查已通过,python,Python,我一直在学习如何像计算机科学家一样思考,我遇到了这个错误。代码如下: class Card: suitList = ["Clubs", "Diamonds", "Hearts", "Spades"] rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] def __init__(self, suit=0,

我一直在学习如何像计算机科学家一样思考,我遇到了这个错误。代码如下: class Card: suitList = ["Clubs", "Diamonds", "Hearts", "Spades"] rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]

def __init__(self, suit=0, rank=2):
    self.suit = suit
    self.rank = rank

def __str__(self):
    return (self.rankList[self.rank] + " of " +
            self.suitList[self.suit])

def __cmp__(self, other):
    if self.suit == 1: return 1
    if self.suit > other.suit: return 1
    if self.suit < other.suit: return -1
    if self.rank > other.suit: return 1
    if self.rank < other.suit: return -1
    return 0
班长(甲板): definit(self,name=“”): self.cards=[] self.name=名称

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
hand.removeMatches()
File "C:\Users\Lin\projects\thinkcs\cardgame.py", line 89, in  removeMatches
self.cards.remove(match)
ValueError: list.remove(x): x not in list
班级纸牌游戏: def初始化(自身): self.deck=deck() self.deck.shuffle()

类别老手(手): def removeMatches(自): 计数=0 原始卡片=自我卡片[:] 对于原始卡中的卡: 匹配=卡片(3-卡片.套装,卡片.等级) 如果在自助卡中匹配: self.cards.remove(卡) self.cards.remove(匹配) 打印“手%s:%s匹配%s%”(self.name、card、match) 计数=计数+1 返回计数

我得到的错误是:

def __cmp__(self, other):
    if self.suit == 1: return 1
    if self.suit > other.suit: return 1
    if self.suit < other.suit: return -1
    if self.rank > other.suit: return 1 # Are you sure you want to compare rank with suit?
    if self.rank < other.suit: return -1 # Are you sure you want to compare rank with suit?
    return 0
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
hand.removeMatches()
removeMatches中第89行的文件“C:\Users\Lin\projects\thinkcs\cardgame.py”
self.cards.remove(匹配)
ValueError:list。删除(x):x不在列表中

我发现
类的
\uu cmp\uu()函数中有一个问题-

def\uuuu cmp\uuuu(自身、其他):
如果self.suit==1:返回1
如果self.suit>other.suit:返回1
如果self.suitother.suit:返回1#是否确实要将等级与西装进行比较?
如果self.rank

您确定要将等级与西装进行比较吗?也许这就是问题的原因?

错误来自哪一行?您对函数的输入是什么(如self.cards),如果没有这些信息,我们无法帮助您给定代码,我将得到
NameError:name'Hand'未定义
,也不是您提供的错误。请调整代码以匹配您的错误。已编辑。请回答。card.suit的样本是什么?@lyx您还没有包括所有代码。我们无法调试不在这里供我们阅读的代码。首先,您需要包括
Hand
类。其次,根据您提供的输出,错误在第89行,但您甚至没有包含80行代码。谢谢我错了,很高兴这对你有用。
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
hand.removeMatches()
File "C:\Users\Lin\projects\thinkcs\cardgame.py", line 89, in  removeMatches
self.cards.remove(match)
ValueError: list.remove(x): x not in list
def __cmp__(self, other):
    if self.suit == 1: return 1
    if self.suit > other.suit: return 1
    if self.suit < other.suit: return -1
    if self.rank > other.suit: return 1 # Are you sure you want to compare rank with suit?
    if self.rank < other.suit: return -1 # Are you sure you want to compare rank with suit?
    return 0