I am writing a python program just to make a simple poker game. I just want to become familiar with the Python language. I know my method of writing the python code isn't exactly Pythonic but really I'm just trying to figure out why my list is appending the last object repeatedly to each space in the List. As I am appending, the list has the correct values but after it just appends the last object repeatedly. I've spent about two days trying to figure it out and I'm sure it's just something I'm overlooking with the language. Any help would be appreciated. Here is the code:
class Card(object):
suit = ""
value = ""
def __init__(self, suit, value):
Card.suit=suit
Card.value=value
def createDeck():
suit = ["DIAMONDS", "HEARTS", "CLUBS","SPADES"]
value = ["ACE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN", "EIGHT", "NINE", "TEN",
"JACK", "QUEEN", "KING"]
Deck = []
for i in range(4):
for j in range(13):
card = Card(suit[i],value[j])
Deck.append(card)
print(Deck[i].value, "OF", Deck[i].suit)
displayDeck(Deck)
def displayDeck(Deck = [], *args):
for i in range(len(Deck)):
print(Deck[i].value, " OF ", Deck[i].suit)
This is the Output I get, it's shortened for brevity: C:\Users\root\Desktop>py Poker.py
ACE OF DIAMONDS
-All Cards Through-
KING OF SPADES
KING OF SPADES
KING OF SPADES
KING OF SPADES
KING OF SPADES
etc. Until The list is filled (52 spots)
In your
Card.__init__
-method you set class attributes instead if instance attributes. So every Card instance has the same attributes, the last one set (King Of Spades). So, set instance attributes withself.
:So one usage of class attributes would be the constants of suit and value names:
Try it like this
This is how you properly set suit and value to Card object.
I deleted the print statement inside the loops. Now the output is:
First of all, you can fix your
Card
class like this:This will solve your problem of the last card being displayed a bunch of times, because you are no longer constantly modifying the same
Card
object. But after that fix, you would have run into another problem because of your print statment. It's because on each loop ofj
, you are using thei
th card in the deck, buti
has not been incrememnted untilj
has increased by13
. This would have been better:But obviously is messy. You want:
To elaborate Rockybilly's answer, your
Card
class is defining two class attributes,suit
andvalue
. Every instance of yourCard
class shares these two values, meaning if you change it in one instance, it is changed in all instances. That's what's happening currently in yourCard.__init__
method when you do something likeCard.suit = suit
. You're modifying the suit for allCard
s you've ever created. In your case, the King of Spades is the last card you create, so its suit and value is the one that gets set for all 52 cards you've created.What you want to do is treat
suit
andvalue
as instance values, meaning you refer to them within your class viaself
. Furthermore, you don't need to set their values at the class level (i.e., right below theclass Card
line). Instead, you just initialize them within your__init__
method. Try changing yourCard
class to this: