2015-12-23 228 views
1

我正在寫一個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) 
+3

查看'createDeck'中的'print'語句;你爲每件套裝執行相同的陳述13次。 – chepner

+1

'Card.suit = suit' - 你是從哪裏得到這個想法的,你是如何設置你的實例屬性的? – user2357112

回答

1

嘗試像這樣

def __init__(self, suit, value): 
     self.suit=suit 
     self.value=value 

這就是你如何正確設置套裝和價值卡對象。

for i in range(4): 
    for j in range(13): 
     card = Card(suit[i],value[j]) 
     Deck.append(card) 
displayDeck(Deck) 

我刪除了循環內的打印語句。 現在輸出的是:

('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] 
+0

但是你沒有在任何地方定義'displayDeck'。對於需要兩行代碼的東西,編寫函數也不是真的需要。或甚至一行。 – Anonymous

+0

它由問題的作者定義。我只是展示了變化的部分。 – Rockybilly

+0

對不起,我看到 – Anonymous

0

首先,可以解決您的Card類是這樣的:

class Card(object): 
    def __init__(self, suit = None, value = None): 
     self.suit=suit 
     self.value=value 

這將解決您最後卡的問題正在顯示一串倍,因爲你不再不斷地修改相同的Card對象。但在修復之後,由於您的印刷資料,您將遇到另一個問題。這是因爲在j的每個循環中,您使用的牌中有i個牌,但i尚未增加,直到j增加13。這會更好:

current = 0 
for i in range(4): 
    for j in range(13): 
     card = Card(suit[i],value[j]) 
     Deck.append(card) 
     print(Deck[current].value, "OF", Deck[current].suit) 
     current = += 1 

但顯然是凌亂。你想要:

for i in range(4): 
    for j in range(13): 
     card = Card(suit[i],value[j]) 
     Deck.append(card) 
display_deck(Deck) 
+0

'i * j'?這不能解決問題,更好''print(card.value,「OF」,card.suit)' – Copperfield

+0

對不起,應該是'i *(j + 1)'。是的,這將解決問題。它與查找給定列和行號的項目索引的方式相同。但我仍然認爲第二個選擇是更好的選擇 – Anonymous

+0

沒關係,我再次改變它。它更容易申報一個新的計數器變量,而不是在處理'i'和'j'時處理''0' – Anonymous

2

在你的Card.__init__ -method你設置類屬性,而不是實例屬性。所以每個卡片實例都有相同的屬性,最後一組(黑桃王)。因此,設置實例與self.屬性:

class Card(object): 
    def __init__(self, suit, value): 
     self.suit = suit 
     self.value = value 

因此類屬性的一個用法是西裝和值名稱的常量:

class Card(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 

def create_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 

def display_deck(deck): 
    for card in deck: 
     print(card.value, " OF ", card.suit) 

deck = create_deck() 
display_deck(deck) 
+0

錯誤對象naem卡在'Cards.VALUES:'中用'in'表示價值。此外,我同意這種方法,更清潔,以保持在'卡'類 – Anonymous

1

要精心Rockybilly的回答,您Card類定義兩個屬性屬性,suitvalue。您的Card類的每個實例共享這兩個值,這意味着如果您在一個實例中更改它,則會在所有實例中更改它。當您執行類似Card.suit = suit的操作時,這就是目前Card.__init__方法中發生的情況。您正在修改您創建的所有Card的套裝。在你的情況下,黑桃王是你創造的最後一張牌,所以它的套裝和價值是爲你創造的所有52張牌設置的。

你想要做的是治療suitvalue例如值,這意味着你通過self類中引用它們。此外,您不需要在課程級別設置它們的值(即,在class Card行的正下方)。相反,您只需在__init__方法中初始化它們。嘗試將Card類更改爲:

class Card(object): 
    def __init__(self, suit, value): 
     self.suit = suit 
     self.value = value 
+0

類的東西謝謝,我擔心沒有使用確切的術語。這就是爲什麼我的解釋很簡短。 – Rockybilly

+0

非常感謝你們,我知道這是我忽略的東西。我以爲你可以在構造函數中使用對象名稱。它現在就像一種魅力!再次,謝謝你! – Zerok00l

相關問題