我是Python OOP的新手,我正在构建一个Blackjack游戏,以提高我对OO Python的理解和实现。
这是我到目前为止为我的Deck和Card课程所写的内容:
import random
class Deck:
def __init__(self):
# init variables
self.cards = []
self.numCards = 0
self.populate_cards()
def deal(self):
return self.cards.pop(random.randint(0, (self.numCards - 1)))
def populate_cards(self):
for suit in range (0, 5):
for face in range (0, 13):
card = Card(face, suit)
self.cards.append(card)
self.numCards = self.numCards + 1
def cards_left(self):
print (self.numCards)
print (self.cards)
class Card:
def __init__(self, face = 0, suit = 0):
self.face = face
self.suit = suit
def assign_value_using_face(self):
pass
当我执行Python提示时,我会在将Cards.py文件导入Python后尝试创建Deck实例。
>>> from Cards import *
>>> deck = Deck()
但是,我一直收到错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Cards.py", line 8, in __init__
self.populate_cards()
AttributeError: Deck instance has no attribute 'populate_cards'
我浏览了Stackoverflow,发生此错误的所有情况都没有正确使用&#34; self。&#34;在populate_cards()前面,或者没有通过&#34; self&#34; populate_cards的参数,我正在做的两件事。有人可以帮忙吗?提前谢谢!