Question: PYTHON CARD PROJECT Define a class where one object of the class represents a playing card. Your class should have the following methods: __init__(self, rank,
PYTHON CARD PROJECT
Define a class where one object of the class represents a playing card. Your class should have the following methods:
__init__(self, rank, suit)
where rank is a number in the range 1-13 indicating the ranks Ace through King, and suitis a single character "d" "c", "h", or "s" indicating the suit (diamonds, clubs, hearts, or spades). This __init__() method initializes a single new Card object by storing the number for its rank, and storing the character for its suit in instance variables.
getRank(self)
Returns the rank of the card. This will be the number that the rank was initialized to when the Card object was created.
getSuit(self)
Returns the suit of the card. This will be the character that the suit was initialized to when the Card object was created.
bjValue(self)
Returns the Blackjack value of a card. Ace counts as 1, face cards count as 10, all other cards count as their rank. The returned value from bjValue() must be a number.
__str__(self)
Returns a string that contains the name of the card. For example. "Ace of Spades".
You must also write a simple test program that proves that all the methods work on a few card values. Your test program might be in a different source code file or it might be in the same source code file as the definition of class Card.
Important Notes:
1) Class Card must have a comment that tells what one object of class Card represents.
2) Every method in class Card must have a comment that tells what the method does. For example, the comment must tell what the method returns, if anything.
3) A method named __str__() is special in Python. If asked to convert an object into a string, Python calls this method automatically to do the conversion.
Here is the start of a test program, but the test program you submit must test more cards to make sure that everything works:
c = Card(1, "s") print (c) print (c.getRank()) print (c.getSuit()) print (c.bjValue())
#### OUTPUT ##### Ace of Spades 1 s 1
BELOW IS MY CODES. What should I do to make it work and fulfill the requirement above?
# object of the class Card stores data i.e. ranks, suit, face up cards, black jack values class Card: # constructs a card each time a card is called def __init__(self, rank, suit): self.rank = 0 self.suit = 0 self.bjValue = 0 #set the object of c Card c = Card() print ("Suit", self.suit) print("Rank", self.rank) # Sets the object's data to ranks and suits def setData(self, rank, suit): self.rank = rank self.suit = suit # specifies rank # self.rank = ["Ace", "King", "Queen", "Jack", 10, 9, 8, 7, 6, 5, 4, 3, 2] self.rank = ["Ace"] # specifies suit # self.suit = {"d":"Diamonds","s":"Spades","c":"Clubs","h":"Hearts" } self.suit = {"s":"Spades"} #specify blackjacks self.bjValue = [1,2,3,4,5,6,7,8,9,10] # function #main constructor method def getRank(self): return self.rank def getSuit(self): return self.suit def bjValue(self): return self.bj # this function covert objects to strings def __str__(self): # return "%s of %s" % (self.rank[self.rank], self.suit[self.suit], self.BJValue()) return "%s of %s" % (self.ranks[self.rank], self.suits.get(self.suit)) # return ("%s of %s" % (Ace, Spade) if c == 1: sr = "s" Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
