我正在寫一個python程序來製作一個簡單的撲克遊戲。我只想熟悉Python語言。我知道我編寫Python代碼的方法並不完全是Pythonic,但實際上我只是想弄清楚爲什麼我的列表重複地將最後一個對象附加到列表中的每個空間。正如我追加的那樣,該列表具有正確的值,但是它只是重複追加最後一個對象。我花了大約兩天的時間試圖弄清楚,我確信這只是我忽視語言的一些東西。任何幫助,將不勝感激。下面是代碼:Python列表重複整個列表的最後一個元素
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)
這是輸出我得到的,它縮短爲簡潔: C:\用戶\ ROOT \桌面> 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)
查看'createDeck'中的'print'語句;你爲每件套裝執行相同的陳述13次。 – chepner
'Card.suit = suit' - 你是從哪裏得到這個想法的,你是如何設置你的實例屬性的? – user2357112