0

Python は初めてなので、助けが必要です。私は宿題としてブラックジャック プログラムを書いていましたが、動作していると思いますが、実行するたびに「自己」に何も提供していないと文句を言います。必要ないと思った?完全なコードは次のとおりです。

class BlackjackPlayer:
    '''Represents a player playing blackjack
    This is used to hold the players hand, and to decide if the player has to hit or not.'''
    def __init__(self,Deck):
        '''This constructs the class of the player.
        We need to return the players hand to the deck, get the players hand, add a card from the deck to the playters hand, and to allow the player to play like the dealer.
        In addition to all of that, we need a deck.'''
        self.Deck = Deck
        self.hand = []

    def __str__(self):
        '''This returns the value of the hand.'''
        answer = 'The cards in the hand are:' + str(self.hand)
        return(answer)

    def clean_up(self):
        '''This returns all of the player's cards back to the deck and shuffles the deck.'''
        self.Deck.extend(self.hand)
        self.hand = []
        import random
        random.shuffle(self.Deck)

    def get_value(self):
        '''This gets the value of the player's hand, and returns -1 if busted.'''
        total = 0
        for card in self.hand:
            total += card
        if total > 21:
            return(-1)
        else:
            return(self.hand)

    def hit(self):
        '''add one card from the Deck to the player's hand.'''
        self.hand.append(self.Deck[0])
        self.Deck = self.Deck[1:]
        print(self.hand)

    def play_dealer(self):
        '''This will make the player behave like the dealer.'''
        total = 0
        for card in self.hand:
            total += card
        while total < 17:
            BlackjackPlayer.hit()
            total += BlackjackPlayer[-1]
            print(self.hand)
        if self.hand > 21:
            return -1
        else:
            return total

これを実行すると、次のようになります。

TypeError: get_value() missing 1 required positional arguments: 'self'

助けていただければ幸いです。ここに来るのは初めてですので、ルールや何かを破っていたら申し訳ありません。

4

2 に答える 2

3

あなたが実際にどこにも電話 していないので、あなたが示したコードに問題があるかどうかはわかりません。get_value()

このクラスの使用方法に関係があります。このクラスのオブジェクトをインスタンス化し、それを使用して関数を呼び出すようにする必要があります。そうselfすれば、自動的に引数リストの前に付けられます。

例えば:

oneEyedJim = BlackJackPlayer()
score = oneEyedJim.get_value()

その上、あなたの得点は、エースがソフト (1) またはハード (11) であるという事実を考慮していないようです。

于 2013-05-22T05:34:36.610 に答える
0

BlackjackPlayer.hit()あなたを悩ませている原因かもしれません。クラスの関数を使用する場合は、そのクラスのインスタンスを作成する必要があります。ただし、クラスから関数を呼び出す場合は、次のように簡単に実行できます。

self.hit()

また:

total += BlackjackPlayer[-1]

ここで何を意図しているのかわかりませんが、handリストにアクセスしたい場合は、次のようにします。

total += self.hand[-1]
于 2013-05-22T05:34:54.623 に答える