Python中二十一点中的Deck类

问题描述 投票:0回答:2

我目前正在为基于文本的二十一点游戏开发Deck(用于玩纸牌)类,尽管我的嵌套字典可以工作,但在使用卡套类打印纸牌时遇到了一些困难(我已经测试过)。

代码如下:

import random

cards  = {'Hearts': {'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}, 
          'Diamonds': {'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},
          'Spades': {'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},
          'Clubs': {'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 Deck:

    def __init__(self):
        self.deck = []
        for outer_key in cards:
            'Outer Key = ',outer_key
            for inner_key in cards[outer_key]:
                self.deck.append(('{} of {}'.format(inner_key,outer_key))

    def __str__(self):
        x = ''
        for card in self.deck:
            x += '\n' + card.__init__()
        return 'The deck has: ' + x 

test_deck = Deck()
print(test_deck)
python oop blackjack
2个回答
1
投票

您的代码有两个问题-括号不匹配,您不需要调用card.__init__()


0
投票

[card.__init__()NoneType,您不能将str与它连接在一起。

© www.soinside.com 2019 - 2024. All rights reserved.