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

  • Home
  • SEARCH
  • 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 9069349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:24:27+00:00 2026-06-16T17:24:27+00:00

I have built a class below with a constructor. The idea is that a

  • 0

I have built a class below with a constructor. The idea is that a PlayingCard object should be generated randomly by default if a rank and/or suit is not specified. In the event that an invalid suit or rank is specified, an object should not be created. I believe this is working, but I am not sure what to make of the exception. I wanted to verify that the only exception thrown in case of an invalid rank or suit is the one that I specified. I am confused why there is a ‘NameError’ that says the instance has no attribute ‘suit’ as I did not specify this when I wrote the raise statement.

Also, I would like to make the class flexible so that user defined rank and suit do not need to be passed in as strings when creating an instance, but I cannot seem to get it to work. If anybody could point me in the right direction or explain why I would not want to do this, it would be appreciated.

class PlayingCard:
    ranks = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
    suits = ['Spades', 'Hearts', 'Clubs', 'Diamonds']

    def __init__(self, rank = None, suit = None):
        if rank is None: self.rank = PlayingCard.ranks[rand.randint(0,12)]
        elif rank in PlayingCard.ranks: self.rank = rank


        if suit is None: self.suit = PlayingCard.suits[rand.randint(0,3)]
        elif suit in PlayingCard.suits: self.suit = suit


        if self.rank not in PlayingCard.ranks or self.suit not in PlayingCard.suits: raise NameError('Invalid Suit or Rank')


>>> c1 = PlayingCard('15','Diamonds')

Traceback (most recent call last):
  File "<pyshell#63>", line 1, in <module>
    c1 = PlayingCard('15','Diamonds')
  File "C:\Python27\poker.py", line 26, in __init__
    if self.rank not in PlayingCard.ranks or self.suit not in PlayingCard.suits: raise NameError('Invalid Suit or Rank')
AttributeError: PlayingCard instance has no attribute 'rank'

>>> c2 = PlayingCard('A', 'Swords')

Traceback (most recent call last):
  File "<pyshell#64>", line 1, in <module>
    c2 = PlayingCard('A', 'Swords')
  File "C:\Python27\poker.py", line 26, in __init__
    if self.rank not in PlayingCard.ranks or self.suit not in PlayingCard.suits: raise NameError('Invalid Suit or Rank')
AttributeError: PlayingCard instance has no attribute 'suit'
  • 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-16T17:24:28+00:00Added an answer on June 16, 2026 at 5:24 pm

    When rank is not None and also not in ranks, self.rank is never set and so

    if self.rank not in PlayingCard.ranks
    

    won’t work; self.rank doesn’t exist. Same goes for suit. You could be more specific with the error and solve the problem all at once:

    def __init__(self, rank = None, suit = None):
        if rank is None: self.rank = PlayingCard.ranks[rand.randint(0,12)]
        elif rank in PlayingCard.ranks: self.rank = rank
        else: raise NameError('Invalid rank')
    
        if suit is None: self.suit = PlayingCard.suits[rand.randint(0,3)]
        elif suit in PlayingCard.suits: self.suit = suit
        else: raise NameError('Invalid suit')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a POCO class that is mostly built through a service. I hit
I have some code below. This code is a basic push/pop stack class that
I have built a class in PHP and I must declare a class variable
I have built up an array of objects, created from a class, I wrote
I have a Windows service built upon ATL 7's CAtlServiceModuleT class. This service serves
i've built a BaseAdapter extended class for my list view.. inside i have a
I have a custom class that I use to build table strings. I would
I have an abstract class CommandPath, and a number of derived classes as below:
I have built my own SQL Query builder that breaks apart an Expression, however,
I have built a database helper class with an open() method and extended sqlite

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.