Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:01:17+00:00 2026-06-14T21:01:17+00:00

I have a program ( blackjack.py ) and it accesses another program’s ( cards.py

  • 0

I have a program (blackjack.py) and it accesses another program’s (cards.py and games.py) within its code. Most of this is from a book, so I’m having trouble understanding how it works.

Heres the code for cards.py:

class Card(object):
    """ A playing card. """
    RANKS = ["A", "2", "3", "4", "5", "6", "7",
             "8", "9", "10", "J", "Q", "K"]
    SUITS = ["c", "d", "h", "s"]
    def __init__(self, rank, suit, face_up = True):
        self.rank = rank
        self.suit = suit
        self.is_face_up = face_up

    def __str__(self):
        if self.is_face_up:
            rep = self.rank + self.suit
        else:
            rep = "XX"
        return rep

    def flip(self):
        self.is_face_up = not self.is_face_up


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)


class Deck(Hand):
    """ A deck of playing cards. """
    def populate(self):
        for suit in Card.SUITS:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self):
        import random
        random.shuffle(self.cards)

    def deal(self, hands, per_hand = 1):
        for round in range(per_hand):
            for hand in hands:
                if self.cards:
                    top_card = self.cards[0]
                    self.give(top_card, hand)
                else:
                    print "Can't continue deal. Out of cards!"

if __name__ == "__main__":
    print "This is a module with classes for playing cards."
    raw_input("\n\nPress the enter key to exit.")

I’m writing an error check for the blackjack.py and I need to gather the number of cards that have been used so far. I think I can do that by accessing the number of values in cards[]. The problem is, I am not sure on how to do that.

This is all in theory though. I will include the `blackjack.py’ code as well, so you all can see what I am trying to do, and help me determine if my logic is flawed.

blackjack.py code

Any and all input is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:01:18+00:00Added an answer on June 14, 2026 at 9:01 pm

    While I’m not entirely clear on your intended structure here, you have a couple of options.

    First, in order to use any functions or classes from cards.py in your blackjack.py module, you can use the import statement to import them. There are two styles of doing this:

    # blackjack.py
    import cards
    

    Would give you access to everything in the cards module, and you’d call each function/class by prefacing it with cards.<name>. So, if you wanted to initialize an instance of the Deck class,

    # blackjack.py
    import cards
    mydeck = cards.Deck()
    

    The other way is the from <X> import <Y>, which gives you access to the functions and classes without having to add the prefix. Example:

    # blackjack.py
    from cards import Deck  # or, to import everything, "from cards import *"
    mydeck = Deck()
    

    Either of these methods would give you an instance of the cards.Deck class.

    Option 0

    You can already tell this within your Deck class, since it subclasses from Hand, so every time you give a card it’s taking out out of the Deck‘s cards attribute. Thus, the number of cards given out would simply be:

    class Deck(Hand):
        # ...
        def number_cards_used(self):
             return 52 - len(self.cards)
    

    Alternatively, if you can’t edit cards.py, you can simply get the number of cards left from your given Deck by:

    # blackjack.py
    def get_number_cards_used_from_deck(deck):
        return 52 - len(deck.cards)
    

    In use:

    # blackjack.py
    import cards
    mydeck = cards.Deck()
    # ...
    # Do other operations with deck
    # ...
    cards_used = get_number_cards_used_from_deck(mydeck)
    

    Option 1

    If you can isolate all of and only those hands being played simultaneously, you can implement a method card_count:

    class Hand(object):
       # ...
       # other code
       # ....
    
       def card_count(self):
           return len(self.cards)
    

    Then, if you have a list of all the hands, for example, you could do something like:

    sum(map(lambda h: h.card_count(), list_of_hands))
    

    Option 2

    Within your Deck class, since it subclasses Hand, you could simply keep another running list of the cards that have been given out that would often be refreshed. This would look like:

    class Deck(Hand):
        # ...
        # other code
        # ...
    
        def __init__(self):
            self.populate()
            self.used_cards = []
    
        def give(self, card, other_hand):
            self.used_cards.append(card)
            super(Deck, self).give(card, other_hand)
    
        def number_cards_used(self):
            return len(self.used_cards)
    

    There are other methods, of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have program which prints all char from char_min to char_max here is code
I have the following code in my program for a blackjack game: Player *p;
I have a program that gets a JSON from the server using getJSON and
Here is my situation: I have program A which looks like this: Fmfile IF
I'm confused. I have program which I run as a non-administrator user. This program
I have program, that has structure defined like this: struct foo { int magic;
I have program output that looks like this (tab delim): $ ./mycode somefile 0000000000000000000000000000000000
i have program like this private void do_my_method_click(object sender, RoutedEventArgs e) { //there are
I have program use c and c++ language to access data from MySQL database.
In my program I put this code for instructs program to spawn a command.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.