重复从类对象(Python)中的列表中删除项(repeatedly remove items from list in class-object (Python))

如何在Python 3中重复删除列表中的项目? 对于我的代码工作的第一项,但如果我尝试应用再次删除项的方法,则会产生TypeError 。 这是我正在使用的代码:

BlackJack游戏

from random import choice class Black_jack_deck(object): def __init__(self, full_deck=(([str(i) for i in range(2,11)]+["A","J","Q","K"])*4*6)): #a deck of 312 cards containing 6* 52 french cards self.full_deck = full_deck def draw(self, draw=True): self.draw = choice(self.full_deck) #I use choice to simulate the dealer drawing a card from the top of a mixed card staple return self.draw self.full_deck = (self.full_deck).remove(self.draw) deck = Black_jack_deck()

我尝试第二次调用deck.draw()时产生的错误如下所示:

Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> deck.draw() TypeError: 'str' object is not callable

注意:即使没有choice()函数,例如在洗牌的full_deck上使用pop() ,也会发生同样的错误。

How can I remove items from a list repeatedly in Python 3? For the first item my code works, but if I try to apply the method that removes items again it produces a TypeError. Here is the code that I am working with:

BlackJack Game

from random import choice class Black_jack_deck(object): def __init__(self, full_deck=(([str(i) for i in range(2,11)]+["A","J","Q","K"])*4*6)): #a deck of 312 cards containing 6* 52 french cards self.full_deck = full_deck def draw(self, draw=True): self.draw = choice(self.full_deck) #I use choice to simulate the dealer drawing a card from the top of a mixed card staple return self.draw self.full_deck = (self.full_deck).remove(self.draw) deck = Black_jack_deck()

The error that is produced when I try to call deck.draw() for a second time looks like this:

Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> deck.draw() TypeError: 'str' object is not callable

Note: even without the choice() function, for instance using pop() on a shuffled full_deck, the same error occurs.

最满意答案

你用绘制的卡覆盖了Black_jack_deck.draw()方法。 因此, deck.draw()的错误是一个'str' object , is not callable 。

这是一个较短的版本。 你需要记住鞋子里的卡片吗? 我删除了draw()的boolean参数,因为我不知道它为什么存在。

In [94]: class CardShoe(object): ...: def __init__(self, num_decks=6): ...: self.cards = list('A23456789JQK' * 4 * num_decks) ...: random.shuffle(self.cards) ...: ...: def draw(self): ...: self.last_card_drawn = self.cards.pop() ...: return self.last_card_drawn ...: In [95]: shoe = CardShoe() In [96]: shoe.draw() Out[96]: '2' In [97]: shoe.draw() Out[97]: '8'

You were overwriting the Black_jack_deck.draw() method with the card drawn. Hence the error about deck.draw() is a 'str' object and is not callable.

Here's a shorter version. Do you need to remember the drawn card in the shoe? And I removed the boolean argument to draw() because I didn't know why it was there.

In [94]: class CardShoe(object): ...: def __init__(self, num_decks=6): ...: self.cards = list('A23456789JQK' * 4 * num_decks) ...: random.shuffle(self.cards) ...: ...: def draw(self): ...: self.last_card_drawn = self.cards.pop() ...: return self.last_card_drawn ...: In [95]: shoe = CardShoe() In [96]: shoe.draw() Out[96]: '2' In [97]: shoe.draw() Out[97]: '8'重复从类对象(Python)中的列表中删除项(repeatedly remove items from list in class-object (Python))

如何在Python 3中重复删除列表中的项目? 对于我的代码工作的第一项,但如果我尝试应用再次删除项的方法,则会产生TypeError 。 这是我正在使用的代码:

BlackJack游戏

from random import choice class Black_jack_deck(object): def __init__(self, full_deck=(([str(i) for i in range(2,11)]+["A","J","Q","K"])*4*6)): #a deck of 312 cards containing 6* 52 french cards self.full_deck = full_deck def draw(self, draw=True): self.draw = choice(self.full_deck) #I use choice to simulate the dealer drawing a card from the top of a mixed card staple return self.draw self.full_deck = (self.full_deck).remove(self.draw) deck = Black_jack_deck()

我尝试第二次调用deck.draw()时产生的错误如下所示:

Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> deck.draw() TypeError: 'str' object is not callable

注意:即使没有choice()函数,例如在洗牌的full_deck上使用pop() ,也会发生同样的错误。

How can I remove items from a list repeatedly in Python 3? For the first item my code works, but if I try to apply the method that removes items again it produces a TypeError. Here is the code that I am working with:

BlackJack Game

from random import choice class Black_jack_deck(object): def __init__(self, full_deck=(([str(i) for i in range(2,11)]+["A","J","Q","K"])*4*6)): #a deck of 312 cards containing 6* 52 french cards self.full_deck = full_deck def draw(self, draw=True): self.draw = choice(self.full_deck) #I use choice to simulate the dealer drawing a card from the top of a mixed card staple return self.draw self.full_deck = (self.full_deck).remove(self.draw) deck = Black_jack_deck()

The error that is produced when I try to call deck.draw() for a second time looks like this:

Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> deck.draw() TypeError: 'str' object is not callable

Note: even without the choice() function, for instance using pop() on a shuffled full_deck, the same error occurs.

最满意答案

你用绘制的卡覆盖了Black_jack_deck.draw()方法。 因此, deck.draw()的错误是一个'str' object , is not callable 。

这是一个较短的版本。 你需要记住鞋子里的卡片吗? 我删除了draw()的boolean参数,因为我不知道它为什么存在。

In [94]: class CardShoe(object): ...: def __init__(self, num_decks=6): ...: self.cards = list('A23456789JQK' * 4 * num_decks) ...: random.shuffle(self.cards) ...: ...: def draw(self): ...: self.last_card_drawn = self.cards.pop() ...: return self.last_card_drawn ...: In [95]: shoe = CardShoe() In [96]: shoe.draw() Out[96]: '2' In [97]: shoe.draw() Out[97]: '8'

You were overwriting the Black_jack_deck.draw() method with the card drawn. Hence the error about deck.draw() is a 'str' object and is not callable.

Here's a shorter version. Do you need to remember the drawn card in the shoe? And I removed the boolean argument to draw() because I didn't know why it was there.

In [94]: class CardShoe(object): ...: def __init__(self, num_decks=6): ...: self.cards = list('A23456789JQK' * 4 * num_decks) ...: random.shuffle(self.cards) ...: ...: def draw(self): ...: self.last_card_drawn = self.cards.pop() ...: return self.last_card_drawn ...: In [95]: shoe = CardShoe() In [96]: shoe.draw() Out[96]: '2' In [97]: shoe.draw() Out[97]: '8'