r/learnpython • u/evilrobotboobs • 8h 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 giveni appreciate any and all advice thank you
3
u/mjmvideos 7h ago
Read the error message. It says init takes 1 argument but 2 were given. So either you’re calling it correctly and need to change the method to accept 2, or the method is correct and you need to change where you’re calling it to remove the second argument. Which do you want to do?
0
2
u/MezzoScettico 7h ago edited 7h ago
I'll pile on with the specifics.
Look how you're creating an instance.
deck = Deck(card_deck)So you're expecting to initialize it with a list of cards. Presumably it's supposed to save those cards as an attribute.
But then look how your class initializes it.
def __init__(self):
self.cards = []This initializer does not take an argument containing a list of cards. So there's a mismatch with how you're calling it.
The reason it says you called with 2 arguments even though "Deck(card_deck)" only looks like it has one, is that class instance methods always add "self" as an implied argument before any others.
With that in mind, what do you think should be between the parentheses in init(...)?
Edit: "class methods" was the wrong terminology for a specialized kind of thing you haven't encountered yet. I changed it to "instance methods".
2
u/evilrobotboobs 7h ago
presumably, something to do with cards... i have it as __init__(self, cards) now and it doesn't return an error anymore (yay!) but when i try to print it to check if it worked it gives me <__main__.Deck object at 0x74e12a5e70e0> (what!)
i was facing this issue yesterday and couldn't figure out my way past it, either
1
u/MezzoScettico 7h ago edited 7h ago
What exactly did you try to print? Did you just print deck? This message is printing correctly. It's telling you deck is an object of class Deck. That is a correct statement.
If you want to print what's stored in deck.cards, then you will have to pass deck.cards to the print statement.
Also, when you modified your initializer, did you add code to do something with the cards argument? Because if you didn't, how do you expect Python to know what to do with it?
Edit: By default, Python has no idea what you want to display when you attempt to print your own object, so it just prints the information that it's an object of a certain type. But you can override that behavior and TELL Python how to display it. I do that a lot.
This is an advanced topic, but FYI it's accomplished by adding an __str__ method that returns a string representing your object. You create that representation and you can put anything you want in it. Python doesn't care, it just says "hey, this class has a string conversion method, so it's not my problem any more. Let the conversion method handle it."
"I don't have to know how, let the instance method handle it" is basically a big part of the rationale behind classes.
1
u/evilrobotboobs 6h ago
yeah, but i got it to actually print the list now. i'm kinda very lost in the course right now so i don't really know what's going on at all when i code 😅
1
u/Available_Safe1818 4h ago
That's what all of this sounds like. But you don't seem to have quit and it sounds like you are still trying to resolve things. If you stick with it and resolve these issues and learn from things, it will get better and you'll see that it's all not that hard looking back on it. You should post your whole code if you still have problems.
1
u/Boothiepro 7h ago
That looks correct! Every built in class does have their special
__str__()defined to print something pretty and readable instead of its class and id. lists would also print<List object at id>it's just that theclass Listhas a__str__()defined asfor element in self: print(element)
1
u/Outside_Complaint755 7h 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 7h 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 6h 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.
1
u/GreenGdut 6h ago
well.I'm not laughing at you.But I want say that autually ,for such simple programs,and why don't you ask LLM?It can teach better.(Sorry Im not good at English)
4
u/carcigenicate Carcigenicate 8h ago
Look at your
__init__. You gave it the requiredselfparameter, but if you want to pass in additional information, you need to add another parameter for that data. Right now, you haveself.cards = []and never provided a way for the caller to provide the initial cards.