简体   繁体   English

Python列表重复整个列表的最后一个元素

[英]Python List Repeating Last Element For Whole List

I am writing a python program just to make a simple poker game. 我在编写python程序只是为了制作一个简单的扑克游戏。 I just want to become familiar with the Python language. 我只想熟悉Python语言。 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. 我知道我编写python代码的方法不完全是Pythonic,但实际上我只是想弄清楚为什么我的列表将最后一个对象重复地追加到列表中的每个空格。 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 这是我得到的输出,为简洁起见,以下简称: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. Card.__init__方法中,您设置类属性,而不是实例属性。 So every Card instance has the same attributes, the last one set (King Of Spades). 因此,每个Card实例都具有相同的属性,即最后一组(黑桃王)。 So, set instance attributes with self. 因此,使用self.设置实例属性self. :

class Card(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: 因此,类属性的一种用法是西服和值名称的常量:

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)

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. 这是您如何为Card对象正确设置西服和值。

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]

To elaborate Rockybilly's answer, your Card class is defining two class attributes, suit and value . 为了详细说明Rockybilly的答案,您的Card类定义了两个属性, suitvalue Every instance of your Card class shares these two values, meaning if you change it in one instance, it is changed in all instances. Card类的每个实例都共享这两个值,这意味着如果在一个实例中对其进行更改,则在所有实例中都将对其进行更改。 That's what's happening currently in your Card.__init__ method when you do something like Card.suit = suit . 当您执行Card.suit = suit这样的事情时,这就是Card.__init__方法中当前正在发生的事情。 You're modifying the suit for all Card s you've ever created. 您正在修改自己创建的所有 Card的西装。 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. 在您的情况下,黑桃王是您创建的最后一张牌,因此它的花色和价值是为您创建的所有52张牌设置的。

What you want to do is treat suit and value as instance values, meaning you refer to them within your class via self . 您想要做的是将suitvalue视为实例值,这意味着您可以通过self在类中引用它们。 Furthermore, you don't need to set their values at the class level (ie, right below the class Card line). 此外,您无需在课程级别(即, class Card行的正下方)设置它们的值。 Instead, you just initialize them within your __init__ method. 相反,您只需在__init__方法中对其进行初始化。 Try changing your Card class to this: 尝试将Card类更改为此:

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

First of all, you can fix your Card class like this: 首先,您可以像这样修复Card类:

class Card(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. 这将解决最后一张卡片显示多次的问题,因为您不再需要不断修改同一张Card对象。 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 . 这是因为在j每个循环中,您都在套牌中使用了第i张卡,但是直到j增加13i才被取消。 This would have been better: 这样会更好:

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM