0
class Card:
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value

    def show(self):
#         print(f"{self.value} of {self.suit}")
        return (self.suit, self.value)


class Deck(Card):
    """
    Deck is collection of 52 cards.
    """

    colour = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
    rank = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
    def __init__(self):

        d = []
        for s in Deck.colour:
            for r in Deck.rank:
                c = Card(s,r)
                d.append(c)
        self.pack = d

    def draw(self):
        return self.pack.pop()


mydeck = Deck()
j =1
for i in mydeck.pack:
    print(j, "\t", mydeck.draw().show(), " count remaining ", len(mydeck.pack))
    j +=1

while trying to print the contents of mydeck.deck, which is a list, it only prints half of total values. If run again it prints next half of values.

Please help me to figure out why all content is not printed?

I am a beginner and any feedback is very much appreciated. Thanks in advance.

  • 1
    Are you sure you want `Deck` to inherit from `Card`? I would suggest that this isn't the place for inheritance... You might just want a list of cards stored inside your deck... – Ed Ward Jan 14 '20 at 18:10
  • Possible duplicate: [strange result when removing item from a list](https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list) – manveti Jan 14 '20 at 18:11
  • @EdWard even i am not sure of inheriting the `Card` in `Deck`. The way i am proceeding with this is : creating a class for cards and then creating a deck of card objects. – sillygames Jan 14 '20 at 18:20

2 Answers2

0

Just don't call draw() inside the loop — i is already iterating through each card in mydeck.pack, so you can just call i.show(). You might want to rename i to card.

As Ed Ward pointed out in the comments, you also definitely do not want Deck to inherit from Card.

Personman
  • 2,324
  • 1
  • 16
  • 27
0

I fixed this way :

class Card:
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value

    def show(self):
#         print(f"{self.value} of {self.suit}")
        return (self.suit, self.value)


class Deck():
    """
    Deck is collection of 52 cards.
    """

    colour = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
    rank = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
    def __init__(self):
        d = []
        for s in Deck.colour:
            for r in Deck.rank:
                c = Card(s,r)
                d.append(c)
        self.pack = d

    def draw(self):
        return self.pack.pop()


mydeck = Deck()
j =1
breakpoint()
for i in mydeck.pack:
    print(j, "\t", i.show(), " count remaining ", len(mydeck.pack)-j)
    j +=1

OUTPUT

1        ('Hearts', 'Two')  count remaining  51
2        ('Hearts', 'Three')  count remaining  50
3        ('Hearts', 'Four')  count remaining  49
4        ('Hearts', 'Five')  count remaining  48
5        ('Hearts', 'Six')  count remaining  47
6        ('Hearts', 'Seven')  count remaining  46
7        ('Hearts', 'Eight')  count remaining  45
8        ('Hearts', 'Nine')  count remaining  44
9        ('Hearts', 'Ten')  count remaining  43
10       ('Hearts', 'Jack')  count remaining  42
......................
MasterOfTheHouse
  • 1,164
  • 1
  • 14
  • 34