Python Forum

Full Version: sorting a deck of cards (objects in a list)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all! I am working on an assignment that involves sorting a hand of 20 cards from a deck of cards using bubble sort and insertion sort methods. The code for the deck of cards has already been provided for us (it just ends up being a list of objects of type Card) and should not be edited.
Here:
class Card:
    def __init__(self, value):
        self.__value = value
        self.__suits = ["Spades", "Clubs", "Hearts", "Diamonds"]
        self.__ranks = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"]

    def getRank(self):
        return self.__ranks[self.__value % 13]

    def getSuit(self):
        return self.__suits[self.__value // 13]

    def getCardValue(self):
        return self.__value % 13 + 1

    def getDeckValue(self):
        return self.__value

    def getNickName(self):
        nickName = ""
        if self.getCardValue() > 1 and self.getCardValue() < 11:
            nickName += str(self.getCardValue())
        else:
            nickName += self.getRank()[0]

        nickName += self.getSuit()[0]

        return nickName

    # Dunder to return a string representation to print() and format()
    def __str__(self):
        return self.getRank() + " of " + self.getSuit()

    # Dunder to return a string representation to other usages
    def __repr__(self):
        return self.__str__()


and the deck is created using the following:

import random
from card import Card

class Deck:
    def __init__(self):
        self.shuffle()

    def shuffle(self):
        self.__deck = []
        for i in range(52):
            self.__deck.append(Card(i))
        random.shuffle(self.__deck)

    def draw(self):
        return self.__deck.pop()
I have created a list called playerHand that draws 20 cards from the deck
deck = Deck()
playerHand = []

for i in range(20):
     playerHand.append(deck.draw())
to create a hand of 20 cards that need to be sorted. Because the cards are set as a string: "Two of Hearts", etc...I am not sure how to establish their value so that they can be sorted in order of value. I tried looping through each individual card and checking for the string associated with card value (e.g. "Two") and assigning it an integer value (2), but I get the following error:
Error:
TypeError: argument of type 'Card' is not iterable
Here is the code I was writing in my efforts to accomplish the ability to compare:

  for i in playerHand:
        if "Two" in i:
            value = 2
        elif "Three" in i:
            value = 3
        elif "Four" in i:
            value = 4
        elif "Five" in i:
            value = 5
        elif "Six" in i:
            value = 6
        elif "Seven" in i:
            value = 7
        elif "Eight" in i:
            value = 8
        elif "Nine" in i:
            value = 9
        elif "Ten" or "Jack" or "Queen" or "King" in i:
            value = 10
        else:
            value = 11
Is there a better way to accomplish the comparison so I can sort these objects? Am I just off on a completely wrong track? It seems like the error message is telling me that I cannot iterate through a list of objects?
Help?
you want == i.get_rank().