没有完全理解这一行代码

时间:2019-05-16 06:43:48

标签: python

我想知道以下代码行在做什么,我想在我启动for循环时得到帮助以完全理解上层阶级的情况

    import random

    suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
    ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven','Eight','Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
    values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,
             'Queen':10, 'King':10, 'Ace':11}

    playing = True



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

        def __str__(self):
            return "{} of {}".format(self.rank,self.suit)

    card = Card(suits,ranks)

    class Deck():

        def __init__(self):
            self.deck = []
            for suit in suits:
                for rank in ranks:
                    self.deck.append(Card(suit,rank)) <======= I want to know what is this performing?

        def __str__(self):
            deck_comp = ""
            for card in self.deck:
                deck_comp += "\n" + card.__str__()
            return "The card is" + deck_comp

        def shuffle(self):
            random.shuffle(self.deck)

        def deal(self):
            single_card = self.deck.pop()
            return single_card

    deck = Deck

1 个答案:

答案 0 :(得分:1)

您正在将Card实例添加到列表中。您应该先了解一下自己,然后再提出更具体的问题。很难解释,因为我们不知道您到底知道多少,而且学科很广。相关阅读: