Question about creating classes

# Work on the player class give them the ability to have a hand and to deal the 
# cards into that hand 

from random import shuffle

class Card:
    def __init__(self, rank, suit): 
        self.rank = rank
        self.suit = suit 
    
    def __str__(self):
        return str(self.rank) + " of " + self.suit

class Deck:
    deck = []
    def __init__(self):
    
        suits = "Hearts", "Diamonds", "Clubs", "Spades"
        ranks = 2,3,4,5,6,7,8,9,10,"J","Q","K","A"
        for suit in suits:
            for rank in ranks:
                card= Card(rank,suit)
                self.deck.append(card)
    # need to be able to deal cards
    def deal_card(self, player):
        dealt_card = self.deck.pop()
        player.hand.append(dealt_card)
        return dealt_card # return gives back the value to whomever called it.  

    # needs to be able to shuffle
    def shuffle(self):
        shuffle(self.deck)
    # display the deck
    def display(self):
        for card in self.deck:
            print(card)

class Player:
    def __init__(self, name, isdealer):
        self.name = name
        self.hand = []
        self.isdealer = isdealer

Player("Andrew", False)
deal_card("Andrew")
deal_card("Andrew")

I am trying to test and see if I can get the Player class activated and if I can get my player to draw two cards and put them in my hand.

The error message: NameErorr: name deal_card is not defined

Where am I going wrong? How can I get cards to be drawn?

you have to create a Player and Deck object for that last part to work as intended, because you’re trying to use methods … which usually have an object [an instance of the class] (which is used for the self parameter each time).

deck = Deck()  # create deck object (instance of deck)
andrew = Player("Andrew", False)  # create player object
deck.deal_card(andrew)
deck.deal_card(andrew)
# Work on the player class give them the ability to have a hand and to deal the 
# cards into that hand 

from random import shuffle

class Card:
    def __init__(self, rank, suit): 
        self.rank = rank
        self.suit = suit 
    
    def __str__(self):
        return str(self.rank) + " of " + self.suit

class Deck:
    deck = []
    def __init__(self):
    
        suits = "Hearts", "Diamonds", "Clubs", "Spades"
        ranks = 2,3,4,5,6,7,8,9,10,"J","Q","K","A"
        for suit in suits:
            for rank in ranks:
                card= Card(rank,suit)
                self.deck.append(card)
    # need to be able to deal cards
    def deal_card(self, player):
        dealt_card = self.deck.pop()
        player.hand.append(dealt_card)
        return dealt_card # return gives back the value to whomever called it.  

    # needs to be able to shuffle
    def shuffle(self):
        shuffle(self.deck)
    # display the deck
    def display(self):
        for card in self.deck:
            print(card)

class Player:
    def __init__(self, name, isdealer):
        self.name = name
        self.hand = []
        self.isdealer = isdealer

deck = Deck()
andrew = Player("Andrew", False)
deck.deal_card(andrew)
deck.deal_card(andrew)
print(andrew)

How do I see if the cards have been added to andrew’s hand?

the cards are being added to the .hand list for the Player object andrew
so you’d do
print(andrew.hand)
to see Andrew’s hand

thank you janbazant1107978602 I am really having a difficult time with this and I appreicate your answers. I may be posting throughout the day it seems like. -_-.

it seems I need to use the str method. But that def__str__(self) method is in another class. Do I do str()

# Work on the player class give them the ability to have a hand and to deal the 
# cards into that hand 

from random import shuffle

class Card:
    def __init__(self, rank, suit): 
        self.rank = rank
        self.suit = suit 
    
    def __str__(self):
        return str(self.rank) + " of " + self.suit

class Deck:
    deck = []
    def __init__(self):
    
        suits = "Hearts", "Diamonds", "Clubs", "Spades"
        ranks = 2,3,4,5,6,7,8,9,10,"J","Q","K","A"
        for suit in suits:
            for rank in ranks:
                card= Card(rank,suit)
                self.deck.append(card)
    # need to be able to deal cards
    def deal_card(self, player):
        dealt_card = self.deck.pop()
        player.hand.append(dealt_card)
        return dealt_card # return gives back the value to whomever called it.  

    # needs to be able to shuffle
    def shuffle(self):
        shuffle(self.deck)
    # display the deck
    def display(self):
        for card in self.deck:
            print(card)

class Player:
    def __init__(self, name, isdealer):
        self.name = name
        self.hand = []
        self.isdealer = isdealer
        
# Work on the player class give them the ability to have a hand and to deal the 
# cards into that hand 

def main(): # main can be called blackjack or gameplay
    
# Welcome the player and explain the rules
    print("""Welcome to Blackjack! Here are the Rules
      Try to get as close to 21 without going over.
      Kings, Queens, and Jacks are worth 10 points.
      Aces are worth 1 or 11 points.
      Cards 2 through 10 are worth their face value.
      (H)it to take another card.
      (S)tand to stop taking cards.
      The dealer stops hitting at 17""")

    # Run a game of blackjack
    # create a deck of cards outside of the main. 
    deck = Deck()
    deck.shuffle()
    # deck.display()

    # Make player 1 and the dealer

#    while True:

        # return cards to the deck
        # Shuffle the deck of cards close to the start to start a new game. 
        # Deal 2 cards to the players

        # Loop: display hands and scores 
        # Ask them to hit or stand. 
        # Determine Winner

deck = Deck()
andrew = Player("Andrew", False)
deck.deal_card(andrew)
deck.deal_card(andrew)
print(andrew.hand)



    
# If the program is run (instead of imported), run the game:
if __name__ == '__main__':
    main()

try changing the __str__ to __repr__ in the Card class instead

or you could use a loop:

for card in andrew.deck:
  print(str(card))
from random import shuffle class Card: def __init__(self, rank, suit): self.rank = rank self.suit = suit def __repr__(self): return "Card: " + str(self.rank) + " of " + self.suit class Deck: deck = [] def __init__(self): suits = "Hearts", "Diamonds", "Clubs", "Spades" ranks = 2,3,4,5,6,7,8,9,10,"J","Q","K","A" for suit in suits: for rank in ranks: card= Card(rank,suit) self.deck.append(card) # need to be able to deal cards def deal_card(self, player): dealt_card = self.deck.pop() player.hand.append(dealt_card) return dealt_card # return gives back the value to whomever called it. # needs to be able to shuffle def shuffle(self): shuffle(self.deck) # display the deck def display(self): for card in self.deck: print(card) class Player: def __init__(self, name, isdealer): self.name = name self.hand = [] self.isdealer = isdealer # Work on the player class give them the ability to have a hand and to deal the # cards into that hand def main(): # main can be called blackjack or gameplay # Welcome the player and explain the rules print("""Welcome to Blackjack! Here are the Rules Try to get as close to 21 without going over. Kings, Queens, and Jacks are worth 10 points. Aces are worth 1 or 11 points. Cards 2 through 10 are worth their face value. (H)it to take another card. (S)tand to stop taking cards. The dealer stops hitting at 17""") # Run a game of blackjack # create a deck of cards outside of the main. deck = Deck() deck.shuffle() # deck.display() # Make player 1 and the dealer # while True: # return cards to the deck # Shuffle the deck of cards close to the start to start a new game. # Deal 2 cards to the players # Loop: display hands and scores # Ask them to hit or stand. # Determine Winner deck = Deck() andrew = Player("Andrew", False) deck.deal_card(andrew) deck.deal_card(andrew) print(andrew.hand)
1 Like

How do I call a method from another class?

You could put an object [of the other class] as a parameter for the method,
and then call methods for that object parameter.

link to previous post