Hi,

If the following def shows if a deck is empty, how do I change it to show the number of cards left???

def isEmpty(self):
return (len(self.cards) == 0)

I was thinking just taking the ==0 should work, by returning
number of cards.

macca1111

If you are popping the cards off your deck/pack list, the len() should give the number of cards left. Here is an example:

import random

class Cards(object):
    def __init__(self):
        """creates a deck/pack of cards and shuffles it"""
        suits = ["Hearts", "Diamonds", "Spades", "Clubs"]
        numbers = ["Ace"] + range(2, 11) + ["Jack", "Queen", "King"]
        self.pack = ["%s of %s" % (n, s) for n in numbers for s in suits]
        #print self.pack  # test
        random.shuffle(self.pack)
      
    def deal(self, number_of_cards):
        """
        dealer takes (pops) number_of_cards off the top of the shuffeled deck
        the list of shuffeled cards will eventually run out!
        """
        cardList = [self.pack.pop() for i in range(number_of_cards)]
        #print "Your hand: %s" % cardList
        cards_left = len(self.pack)
        # deck/pack has been dealt reshuffle it
        if cards_left <= 5:
            print "reshuffled!"
            # create and shuffle a new deck
            self.__init__()
        for card in cardList:
            print card

   
newgame = Cards()
deal = "y"
while deal == "y":
    print "-"*30
    newgame.deal(5)
    print "-"*30
    deal = raw_input("Deal again? (y/n) ").lower()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.