Start New Discussion within our Software Development Community


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( == 0)

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


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
    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
        for card in cardList:
            print card

newgame = Cards()
deal = "y"
while deal == "y":
    print "-"*30
    print "-"*30
    deal = raw_input("Deal again? (y/n) ").lower()
This question has already been answered. Start a new discussion instead.