0

I'm writing a text-based Blackjack game in Python 3.5 and have created the following classes and corresponding methods:

import random

class Card_class(object):

    def __init__(self):            
        pass

    def random_card(self):
        suites = ['clubs', 'spades', 'diamonds', 'hearts']
        denomination = ['ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'jack', 'queen', 'king']
        card = (suites[random.randint(0, 3)], denomination[random.randint(0, 13)])     
        return card

    def card_check(self, card, cards_distributed):       
        if card not in cards_distributed:
            cards_distributed.append(card)                
            return True        
        else:                
            return False


class Player_hand(object):

    def __init__(self):            
        pass

    def cards_held(self, card, cards_holding):            
        cards_holding.append(card)            
        return cards_holding

class Distribute_class(object):

    def __init_(self):
        pass

    def initial_card_distribute(self, cards_distributed, player_cards_holding = [], cards_held = 0):

        player = ''
        while cards_held < 2:
            player = Card_class()
            result_card = player.random_card()

            if not player.card_check(result_card, cards_distributed):
                continue
            else:
                Player_hand.cards_held(result_card, player_cards_holding)
                break

        return player_cards_holding

I'm attempting to test my code using

distributed_cards = []
player1 = Distribute_class()
player1_hand = player1.initial_card_distribute(distributed_cards)
player1_hand

But I'm given the following error:

TypeError: cards_held() missing 1 required positional argument:
'cards_holding'

The terminal window which presents the error says the error comes from the line containing Player_hand.cards_held(result_card, player_cards_holding) in the final class, Distribute_class, listed above. Does this line not recognize that I had given it a default parameter of player_cards_holding = [] defined in the method within the same class? Or is there some sort of other problem coming from the fact that the method generating the error, "cards_held", is being called from another class?

jcarder
  • 164
  • 4
  • 17
erik7970
  • 693
  • 1
  • 8
  • 21

1 Answers1

1

You are fundamentally misunderstanding how classes work. The problem is that this line:

Player_hand.cards_held(result_card, player_cards_holding)

Is using the instance method cards_held without passing it an instance (which would be self in your method signature). So, to use the method initialize a Player_hand object like so: ph = Player_hand() and then use

ph.cards_held(result_card, player_cards_holding)

What happens under the hood is that ph get's passed implicitly to cards_held.

Also, watch out for the mutable default argument and don't use it unless you understand how it works.

But fundamentally, you are using classes but not correctly. None of your classes have data attributes! All of these could just be module-level functions and work just as well.

Community
  • 1
  • 1
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172