我正在通过构建二十一点游戏来学习 python(具有 VBA 背景)(是的,我以二十一点为例提出了很多问题)。
这是我的代码:
import random
class DECK():
def load_deck(self):
suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
full_deck = {}
i = 0
for s in suite:
for r in rank:
full_deck[i] = "%s of %s" % (r, s)
i += 1
return full_deck
def pick_item(self, deck):
card_key = random.choice(deck.keys())
new_card = deck[card_key]
del deck[card_key]
return (deck, new_card)
def missing_card(self, deck):
temp_deck = DECK()
print temp_deck
d1 = DECK()
deck1 = d1.load_deck()
deck1, card1 = d1.pick_item(deck1)
print card1
d1.missing_card(d1)
这是我在终端中得到的(文件名hand_c.py
):
$ python hand_c.py
Ace of Clubs
<__main__.DECK instance at 0x10bb0d248>
$
为什么一个功能起作用pick_item
,而另一个不起作用missing_card
?
根据第一个答案,我将函数定义更改为:
def missing_card(self, deck):
deckC1 = DECK()
temp_deck = deckC1.load_deck
print temp_deck
但现在我从终端得到以下信息:
$ python hand_c.py
Jack of Diamonds
<bound method DECK.load_deck of <__main__.DECK instance at 0x10500e248>>
$