Python Forum
sorting a deck of cards (objects in a list)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting a deck of cards (objects in a list)
#1
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?
Reply
#2
you want == i.get_rank().
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to compare two cards games? python_beginner99 7 3,721 Nov-30-2021, 06:53 PM
Last Post: deanhystad
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,407 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Sorting list - Homework assigment ranbarr 1 2,230 May-16-2021, 04:45 PM
Last Post: Yoriz
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,899 May-14-2021, 07:14 AM
Last Post: perfringo
  List of Objects print <__main. Problem Kol789 10 3,539 Jul-21-2020, 09:37 AM
Last Post: DeaD_EyE
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,721 Nov-14-2019, 02:44 AM
Last Post: jefsummers
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 2,294 Dec-19-2018, 06:22 PM
Last Post: ichabod801
  another positional argument error (...and executing objects stored in a list) itmustbebunnies 7 4,207 Nov-16-2018, 07:18 PM
Last Post: itmustbebunnies
  Help with list sorting gonzo620 1 3,138 Oct-16-2018, 02:58 PM
Last Post: j.crater
  Sorting list of names by first two characters Otbredbaron 2 3,280 May-24-2018, 03:59 PM
Last Post: Otbredbaron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020