This is exercise 18.2 from "Think Python" book. There is a Class called Deck that creates a deck of cards as a list of tuples [(0,1), (0, 2)...]. The class has defined inner functions shuffle() and sort(). The shuffle() function works correct but when I try to sort the deck the fuction sort() I wrote does not return sorted deck.
I have no idea why is that. Any hint what am I doing wrong?
class Deck(object):
"""Represents deck of cards
attributes: list of cards
"""
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1, 14):
card = Card(suit, rank)
self.cards.append(card)
def __str__(self):
res = []
for card in self.cards:
res.append(str(card))
return '\n'.join(res)
def pop_card(self):
return self.cards.pop()
def add_card(self, card):
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards)
def sort(self):
self.cards.sort()
new_deck = Deck()
print '>>>>>>>>>>>>>>>>>>>>new deck:'
print new_deck
new_deck.shuffle()
print '>>>>>>>>>>>>>>>>>>>>shuffled deck:'
print new_deck
new_deck.sort()
print '>>>>>>>>>>>>>>>>>>>>sorted deck:'
print new_deck