Nested Dealer::Card::Deck class Deck.cards 在build方法中获取list,但在shuffle方法中list不见了
Nested Dealer::Card::Deck class Deck.cards gets the list during the build method, but list is gone during shuffle method
我稍微简化了代码,因为它是更大的 flask 项目的一部分。但是问题依然存在:
import random
class Dealer:
def __init__(self):
self.deck = self.Deck()
class Deck:
def __init__(self):
self.cards = []
self.cards = self.build()
def build(self):
for suit in ['Spades', 'Diamonds', 'Hearts', 'Clubs']:
if suit == 'Spades':
suit_url='S.png'
elif suit == 'Diamonds':
suit_url="D.png"
elif suit == "Hearts":
suit_url="H.png"
elif suit == "Clubs":
suit_url="C.png"
for val in [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]:
img_url = (str(val) + suit_url)
self.cards.append(self.Card(val, suit, img_url))
def shuffle(self):
for i in range(len(self.cards)-1, 0, -1):
rand_num = random.randint(0, i)
self.cards[i], self.cards[rand_num] = self.cards[rand_num], self.cards[i]
class Card:
def __init__(self, value, suit, img):
self.value = value
self.suit = suit
self.img = img
dealer = Dealer()
deck = dealer.Deck()
deck.shuffle()
cards list 在 Deck build 方法中显示有效的卡片对象列表,但是当它进入 shuffle 方法时,cards 在调试器中显示 none?
有什么问题:
'self.build()' 方法没有 return 任何东西(作废)它只是更新 'self.cards'。但是 'self.cards' 坐等到 'self.build()' 的输出。
但是输出是 none,当你想使用 'deck.shuffle()' 时,你正在尝试获取 none.
的长度
如何修复:
只需调用构建方法即可填写卡片。
import random
class Dealer:
def __init__(self):
self.deck = self.Deck()
class Deck:
def __init__(self):
self.cards = []
# Just call build method to fill the cards
self.build()
def build(self):
for suit in ['Spades', 'Diamonds', 'Hearts', 'Clubs']:
if suit == 'Spades':
suit_url='S.png'
elif suit == 'Diamonds':
suit_url="D.png"
elif suit == "Hearts":
suit_url="H.png"
elif suit == "Clubs":
suit_url="C.png"
for val in [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]:
img_url = (str(val) + suit_url)
self.cards.append(self.Card(val, suit, img_url))
def shuffle(self):
for i in range(len(self.cards)-1, 0, -1):
rand_num = random.randint(0, i)
self.cards[i], self.cards[rand_num] = self.cards[rand_num], self.cards[i]
class Card:
def __init__(self, value, suit, img):
self.value = value
self.suit = suit
self.img = img
dealer = Dealer()
deck = dealer.Deck()
deck.shuffle()
我稍微简化了代码,因为它是更大的 flask 项目的一部分。但是问题依然存在:
import random
class Dealer:
def __init__(self):
self.deck = self.Deck()
class Deck:
def __init__(self):
self.cards = []
self.cards = self.build()
def build(self):
for suit in ['Spades', 'Diamonds', 'Hearts', 'Clubs']:
if suit == 'Spades':
suit_url='S.png'
elif suit == 'Diamonds':
suit_url="D.png"
elif suit == "Hearts":
suit_url="H.png"
elif suit == "Clubs":
suit_url="C.png"
for val in [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]:
img_url = (str(val) + suit_url)
self.cards.append(self.Card(val, suit, img_url))
def shuffle(self):
for i in range(len(self.cards)-1, 0, -1):
rand_num = random.randint(0, i)
self.cards[i], self.cards[rand_num] = self.cards[rand_num], self.cards[i]
class Card:
def __init__(self, value, suit, img):
self.value = value
self.suit = suit
self.img = img
dealer = Dealer()
deck = dealer.Deck()
deck.shuffle()
cards list 在 Deck build 方法中显示有效的卡片对象列表,但是当它进入 shuffle 方法时,cards 在调试器中显示 none?
有什么问题:
'self.build()' 方法没有 return 任何东西(作废)它只是更新 'self.cards'。但是 'self.cards' 坐等到 'self.build()' 的输出。 但是输出是 none,当你想使用 'deck.shuffle()' 时,你正在尝试获取 none.
的长度如何修复:
只需调用构建方法即可填写卡片。
import random
class Dealer:
def __init__(self):
self.deck = self.Deck()
class Deck:
def __init__(self):
self.cards = []
# Just call build method to fill the cards
self.build()
def build(self):
for suit in ['Spades', 'Diamonds', 'Hearts', 'Clubs']:
if suit == 'Spades':
suit_url='S.png'
elif suit == 'Diamonds':
suit_url="D.png"
elif suit == "Hearts":
suit_url="H.png"
elif suit == "Clubs":
suit_url="C.png"
for val in [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]:
img_url = (str(val) + suit_url)
self.cards.append(self.Card(val, suit, img_url))
def shuffle(self):
for i in range(len(self.cards)-1, 0, -1):
rand_num = random.randint(0, i)
self.cards[i], self.cards[rand_num] = self.cards[rand_num], self.cards[i]
class Card:
def __init__(self, value, suit, img):
self.value = value
self.suit = suit
self.img = img
dealer = Dealer()
deck = dealer.Deck()
deck.shuffle()