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 9175789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:25+00:00 2026-06-17T16:58:25+00:00

I’m currently working through John Zelle’s Python Programming: An Introduction to Computer Science and

  • 0

I’m currently working through John Zelle’s Python Programming: An Introduction to Computer Science and hit a snag in Chapter 10. I’m having a conceptual issue in understanding the why and how of this exercise and require some assistance on how to approach the problem. The exercise asks me to create a program that displays n number of cards using a class named Card and requires the following methods. It should also be callable from within an app that generates n number of random cards:

  1. __init__(self, rank, suit):
  2. getRank(self)
  3. getSuite(self)
  4. BJValue(self)
  5. __str__(self)

As ridiculously easy as this should be, I hit a wall trying to implement this class. I created a simple app that would generate a deck of 52 cards, prompts the user for the number of cards they want, and then populates the hand with those cards. I just can’t see where I would benefit from a card specific class once the hand is generated. Here’s my working code so far:

import random

class Card:
    def __init__(self, rank, suite):
        self.rank = rank
        self.suite = suite

    def getRank(self):
        return self.rank

    def getSuite(self):
        return self.suite

    def BJValue(self):
        if self.rank == 'Ace':
            return 1
        elif self.rank == 'Jack' or self.rank == 'Queen' or self.rank == 'King':
            return 10
        else:
        return int(self.rank)

    def __str__(self):
        return ('{0} of {1}'.format(self.rank, self.suite))


def shuffled_deck():
    deck = []
    for suite in ['Clubs', 'Diamonds', 'Hearts', 'Spades']:
        for num in ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']:
            deck.append([num, suite])
    random.shuffle(deck)
    return deck


def main():
    deck = shuffled_deck()
    hand = []
    print('>> Card Generator v1 <<')
    while True:
        try:
            n = int(input('Please enter the number of cards to display (1-7): '))
        except ValueError:
            print('Invalid input, please enter a number!\n')
        else:
            if n < 1 or n > 7:
                print('Please enter a number between 1-7!\n')
            else:
                break
    print('Your hand is:')
    for i in range(n):
        hand.append(deck[i])



main()

So once I’ve generated the hand of random n cards, I can’t see how I would benefit from using the Card class, or even where to implement it. Since n is a random number between 1-7, I would need n number of variables to store each card object and then assign each variable to an instance of Card. I could show each card in the hand with hand[i] where i iterates through to range(n) without the need of a special Card class, but that’s not what’s expected from this project. I’m looking for advice on how to think about this issue so that I can make use of this required class.

  • 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-17T16:58:26+00:00Added an answer on June 17, 2026 at 4:58 pm

    It looks like the only thing you need to change in your code is to change the line:

    deck.append([num, suite])
    

    to

    deck.append(Card(num, suite))
    

    This makes the deck variable a list of 52 Card objects. That’s useful because Card objects have some built-in functionality that a list of two items (like [num, suite]) doesn’t.


    Two examples: if you add the line

    print hand
    

    after your code, your current code would print something like

    [["Jack", "Clubs"], ["9", "Spades"]]
    

    while your new code would print

    [Jack of Clubs, 9 of Spades]
    

    (The line print "\n".join(map(str, hand))) might be closer to what you want in practice). You can also get the total blackjack value of your hand with the line:

    sum(c.BJValue() for c in hand)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to loop through a bunch of documents I have to put
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.