我想知道下面这行代码的exaclty是做什么的,当我启动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
您正在将卡的实例添加到列表中。这是你应该了解自己,然后问一个更具体的问题。解释它是困难的,因为我们不知道你到底知道多少,主题是广泛的。相关阅读:
Data Structures
Classes
相关问题 更多 >
编程相关推荐