Python List Repeating Last Element For Whole List
Solution 1:
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 with self.
:
classCard(object):def__init__(self, suit, value):
self.suit = suit
self.value = value
So one usage of class attributes would be the constants of suit and value names:
classCard(object):
SUITS = ["DIAMONDS", "HEARTS", "CLUBS","SPADES"]
VALUES = ["ACE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN", "EIGHT", "NINE", "TEN",
"JACK", "QUEEN", "KING"]
def__init__(self, suit, value):
self.suit = suit
self.value = value
defcreate_deck():
deck = []
for suit in Card.SUITS:
for value in Card.VALUES:
card = Card(suit,value)
deck.append(card)
print(card.value, "OF", card.suit)
return deck
defdisplay_deck(deck):
for card in deck:
print(card.value, " OF ", card.suit)
deck = create_deck()
display_deck(deck)
Solution 2:
Try it like this
def__init__(self, suit, value):
self.suit=suit
self.value=value
This is how you properly set suit and value to Card object.
for i in range(4):
for j in range(13):
card = Card(suit[i],value[j])
Deck.append(card)
displayDeck(Deck)
I deleted the print statement inside the loops. Now the output is:
('ACE', ' OF ', 'DIAMONDS')
('TWO', ' OF ', 'DIAMONDS')
('THREE', ' OF ', 'DIAMONDS')
('FOUR', ' OF ', 'DIAMONDS')
('FIVE', ' OF ', 'DIAMONDS')
('SIX', ' OF ', 'DIAMONDS')
('SEVEN', ' OF ', 'DIAMONDS')
('EIGHT', ' OF ', 'DIAMONDS')
('NINE', ' OF ', 'DIAMONDS')
('TEN', ' OF ', 'DIAMONDS')
('JACK', ' OF ', 'DIAMONDS')
('QUEEN', ' OF ', 'DIAMONDS')
('KING', ' OF ', 'DIAMONDS')
('ACE', ' OF ', 'HEARTS')
('TWO', ' OF ', 'HEARTS')
('THREE', ' OF ', 'HEARTS')
('FOUR', ' OF ', 'HEARTS')
('FIVE', ' OF ', 'HEARTS')
('SIX', ' OF ', 'HEARTS')
('SEVEN', ' OF ', 'HEARTS')
('EIGHT', ' OF ', 'HEARTS')
('NINE', ' OF ', 'HEARTS')
('TEN', ' OF ', 'HEARTS')
('JACK', ' OF ', 'HEARTS')
('QUEEN', ' OF ', 'HEARTS')
('KING', ' OF ', 'HEARTS')
('ACE', ' OF ', 'CLUBS')
('TWO', ' OF ', 'CLUBS')
('THREE', ' OF ', 'CLUBS')
('FOUR', ' OF ', 'CLUBS')
('FIVE', ' OF ', 'CLUBS')
('SIX', ' OF ', 'CLUBS')
('SEVEN', ' OF ', 'CLUBS')
('EIGHT', ' OF ', 'CLUBS')
('NINE', ' OF ', 'CLUBS')
('TEN', ' OF ', 'CLUBS')
('JACK', ' OF ', 'CLUBS')
('QUEEN', ' OF ', 'CLUBS')
('KING', ' OF ', 'CLUBS')
('ACE', ' OF ', 'SPADES')
('TWO', ' OF ', 'SPADES')
('THREE', ' OF ', 'SPADES')
('FOUR', ' OF ', 'SPADES')
('FIVE', ' OF ', 'SPADES')
('SIX', ' OF ', 'SPADES')
('SEVEN', ' OF ', 'SPADES')
('EIGHT', ' OF ', 'SPADES')
('NINE', ' OF ', 'SPADES')
('TEN', ' OF ', 'SPADES')
('JACK', ' OF ', 'SPADES')
('QUEEN', ' OF ', 'SPADES')
('KING', ' OF ', 'SPADES')
[Finished in 0.3s]
Solution 3:
To elaborate Rockybilly's answer, your Card
class is defining two class attributes, suit
and value
. Every instance of your Card
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 your Card.__init__
method when you do something like Card.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
and value
as instance values, meaning you refer to them within your class via self
. Furthermore, you don't need to set their values at the class level (i.e., right below the class Card
line). Instead, you just initialize them within your __init__
method. Try changing your Card
class to this:
classCard( object ):def__init__( self, suit, value ):
self.suit = suit
self.value = value
Solution 4:
First of all, you can fix your Card
class like this:
classCard(object):
def__init__(self, suit = None, value = None):
self.suit=suit
self.value=value
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 of j
, you are using the i
th card in the deck, but i
has not been incrememnted until j
has increased by 13
. This would have been better:
current=0for i inrange(4):
for j inrange(13):
card = Card(suit[i],value[j])
Deck.append(card)
print(Deck[current].value, "OF", Deck[current].suit)
current=+=1
But obviously is messy. You want:
for i in range(4):
for j in range(13):
card = Card(suit[i],value[j])
Deck.append(card)
display_deck(Deck)
Post a Comment for "Python List Repeating Last Element For Whole List"