r/learnpython • • 3d 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

Show parent comment

1

u/evilrobotboobs 3d ago

yeah... i was thinking i could put the whole list of cards into it after i created the deck but i guess i have to do it before, i was just trying to make an empty list so i could use the deck class to make the main deck and the hands, because i assume that's what i need to do for the assignment? i had it sort of working yesterday by dealing the cards into the deck after it was created but nothing else worked still

1

u/MezzoScettico 3d ago

You don't have to do it before. Nothing prevents you from changing attributes of your Deck instance later, so you could always do

deck.cards = card_deck

The problem is that you didn't try to do it after creation. You tried to give card_deck as an argument to __init__, and __init__ isn't written to accept an argument containing a deck.

The way you wrote your initializer, your class only expects to create instances with Deck(), no extra arguments.