【发布时间】:2018-07-08 21:13:46
【问题描述】:
我基本上有 3 节课。卡片、甲板和播放器。甲板是一张卡片列表。我正在尝试从牌组中取出一张牌。但是我收到一个 ValueError 说该卡不在列表中。据我了解,它是并且我通过 removeCard 函数传递了正确的对象。我不知道为什么我会收到ValueError。所以简而言之,问题是我需要从卡片列表中删除一个对象(卡片)。
我的问题是,当我尝试从牌组中取出一张牌时,我收到如下错误:
ValueError: list.remove(x): x not in list
这是我目前所拥有的:
Card类:
import random
class Card(object):
def __init__(self, number):
self.number = number
Deck 类(此处抛出错误,在removeCard 函数中):
class Deck(object):
def __init__(self):
self.cards = []
for i in range(11):
for j in range(i):
self.cards.append(Card(i))
def addCard(self, card):
self.cards.append(card)
def removeCard(self, card):
self.cards.remove(card)
def showCards(self):
return ''.join((str(x.number) + " ") for x in self.cards)
Player类:
class Player(object):
def __init__(self, name, hand):
self.name = name
self.hand = hand
main函数:
def main():
deck = Deck()
handA = [Card(6), Card(5), Card(3)]
handB = [Card(10), Card(6), Card(5)]
playerA = Player("A", handA)
playerB = Player("B", handB)
print("There are " + str(len(deck.cards)) + " cards in the deck.")
print("The deck contains " + deck.showCards())
for i in handA:
deck.removeCard(i)
print("Now the deck contains " + deck.showCards())
main()
【问题讨论】:
-
您在标题中提出了一个特定问题,但在您的问题正文中,对您的问题的描述过于宽泛,并且您的代码转储量很大。不确定您的问题到底是什么?
-
对不起,我是第一次发帖。我基本上有3节课。卡片、甲板和播放器。甲板是一张卡片列表。我正在尝试从牌组中取出一张牌。但是我收到一个 ValueError 说该卡不在列表中。据我了解,它是并且我通过 removeCard 函数传递了正确的对象。我不确定为什么会收到 ValueError。所以简而言之,问题是我需要从卡片列表中删除一个对象(卡片)。
标签: python python-3.x list class