[Tutor] Python OOP question

Eric Enoch eenoch at ncsu.edu
Sat Apr 2 17:46:22 EDT 2016


This is my first non-MATLAB programming language so I am having some
trouble with object-oriented programming. Specifically, I am working on a
program that simulates a blackjack game. It was functioning but I have
tried adding some new functionality in allowing players to make bets

To surmise, BJ Player is an object that was a collection of card objects.
Def_init used to just have self and names as parameters, but I added
startingmonies (starting money that the player is wagering.) The superclass
of BJ player is ultimately BJ hand. I am now getting an error when calling
a method to add a card to that hand. Below is where the error o

class BJ_Game(object): """ A Blackjack Game. """ def *init*(self, names,
startingmonies):
self.players = [] for name in names: player = BJ_Player(name) spot =
names.index(name) startingscratch = startingmonies[spot] player_with_money
= (name, startingscratch,0) self.players.append(player_with_money)

This is where the error originates (cards module that is imported into the
BJ module used above):

class Hand(object): """ A hand of playing cards. """ def *init*(self):
self.cards = []

def __str__(self):
    if self.cards:
       rep = ""
       for card in self.cards:
           rep += str(card) + "\t"
    else:
        rep = "<empty>"
    return rep
def clear(self):
    self.cards = []
def add(self, card):
    self.cards.append(card)
def give(self, card, other_hand):
    self.cards.remove(card)
    other_hand.add(card)

This is the error that I get.: line 47, in give other_hand.add(card)
AttributeError: 'tuple' object has no attribute 'add'

I get this error when trying to deal cards. Everything else worked before
so the only real change that I made is adding the bet parameter. I
researched this issue and was not able to gain a strong understanding. Any
assistance would be greatly appreciated. Thank you!


More information about the Tutor mailing list