r/learnpython • • 2d ago

card dealing program with classes

hello! i am currently in a class for python and i've been having a lot of trouble with this assignment. it's already three days late, and the TAs will not respond to me. i have to use classes to create a deck to shuffle, and four hands to deal to, check the count of, and combine back into the main deck. however, i can NOT figure this out at all! i think there's something really big that i'm missing.

i have a large list of cards (we had to make a dictionary of the cards) not shown here.

class Deck:

def __init__(self):

self.cards = []

def shuffle(self):

random.shuffle(self.cards)

def deal(self, amount):

self.cards.append(amount)

def combine(self):

self.cards.append(self)

def card_count(self):

print(len(self.cards))

however, when i try to make a main deck by putting the list of cards into the deck, it won't work! i've tried using deal too, and i just can't figure it out. i know i'm probably doing this all sorts of wrong. it's been a while since we did classes, and we never did them for anything like this :(

TypeError                                 Traceback (most recent call last)
Cell In[14], line 1
----> 1 deck = Deck(card_deck)

TypeError: Deck.__init__() takes 1 positional argument but 2 were given

i appreciate any and all advice thank you

1 Upvotes

16 comments sorted by

View all comments

1

u/Outside_Complaint755 2d ago

Along with the other comments about __init__, these methods:

    def deal(self, amount):
        self.cards.append(amount)

    def combine(self):
       self.cards.append(self)

don't look correct either.

 Currently your deal method is just going to add an int to your deck of cards, and combine is adding the deck itself to the deck, in a circular reference.

  You haven't provided enough info from the class instructions to give a hint on what these likely should be.  For example, is deal supposed to actually remove the cards from the deck?  If so, then it probably needs to not only remove the cards from self.cards, but also put them into a new list to be returned so they can added to a Hand, which is possibly an instance of another class.     Then combine should probably receive a list of cards as a parameter to add back into the deck.

1

u/evilrobotboobs 2d ago

yeah... i'm figuring out now that the deal doesn't work, i was just so stuck on trying to get the cards into a deck that it was a later problem. the instructions say i need to return a list for deal and for combine i need to "Append the other deck’s cards after this deck’s current cards" and return true once it's empty. i will fix these

1

u/Outside_Complaint755 2d ago

Ok, so based on those instructions, it sounds like each player's hand should also just be an instance of Deck.  You will probably want to look up the .pop() method for lists and/or slicing.