Question
In the previous assignment A Card Object, if your code asked for a new Card in this way: c = Card ('c', 7) there is
"In the previous assignment "A Card Object", if your code asked for a new Card in this way:
c = Card ('c', 7)
there is no Error raised, everything seems fine. But then when you call the bjValue() method, or the __str__()
method, they both raise Errors and your program crashes. This is confusing and incorrect.
The __init__() method should not allow a Card object to be initialized with a 'c' for the rank and a 7 for the
suit. More generally, the __init__() method should raise an Error when any inappropriate values are sent into
its parameters. This will ensure that there will never be a meaningless Card object in any program ever.
For this assignment, you will add code to the __init__() method that raises a:
1) TypeError when the type of the first parameter is not an int.
2) TypeError when the type of the second parameter is not a string.
3) ValueError when the value of the first parameter is not in the range 1..13 inclusive.
4) ValueError when the value of the second parameter is not one of the strings in the set {'s', 'h', 'c', 'd'}
Hints
Start this assignment by making any and all modifications suggested by my comments to your previous
assignment. If there is anything I wrote as feedback that you don't understand, please ask in the public
Discussion. Everything that I mention in the comment section of your Assignment in Canvas must result in
either a change to your source code or a post in the Discussion forum.
Write the main program first. This program will attempt to create new Card objects with either an
inappropriate type or an inappropriate value. This main will have several try/except blocks, each one
containing an attempt to create a Card with an inappropriate value.
The main program that tests your class Card must also attempt to create a new Card object with
appropriatevalues in the parameters to the __init__() method. This attempt should, of course, not raise an
The main program that tests your class Card must also attempt to create a new Card object with
appropriatevalues in the parameters to the __init__() method. This attempt should, of course, not raise an error. It is critical that your main program contain many different tests, to see that bad Cards are not allowed
to be created and good Cards ARE allowed to be created.
In order to make sure that the main program is a thorough test, keep all calls to bjValue() and __str__() in
there.
Develop your code iteratively, but be sure to perform regression testing: Write and test code that
catches one of the four Errors at a time, but be sure that your main program performs all tests in one run. In
this way, you will make sure that newly written code that catches one of the Errors doesn't break your
solution that catches one of the previous Errors that you coded for.
This assignment is asking you to change only the __init__() method in class Card, and the main() program
that tests all the functionality of class Card. Do not change any of the other methods in class Card unless
such a change was suggested in my comments to your submission of the previous "A Card Object"
assignment."
Test Code must be
try:
cardOne(#rank, suit)
except # Type or Value Error:
# print message here
Current Program:
class Card (): def __init__ (self, rank, suit): self.rank = rank self.suit = suit def __str__(self): suits = {"d":"Diamonds", "h": "Hearts", "c": "Clubs", "s": "Spades"} ranks = ["Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"] string_temp = "%s of %s" rank_var = ranks[self.rank-1] suit_var = suits.get(self.suit) master_tuple = (rank_var, suit_var) card_name = string_temp %(master_tuple[0], master_tuple[1]) return card_name def getRank (self):return self.rank def getSuit(self): return self.suit def bjValue (self): if 1 <= self.rank and self.rank <=10: return self.rank else: return 10
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started