Question: Help in solving , completing this Python program. For this project I h
ID: 3829156 • Letter: Q
Question
Question: Help in solving , completing this Python program.
For this project I have to implement to the classes the following specifications.
Class Card) Have to Overload appropriate operators for class Card so that you can compare cards based on rank:
>>> Card(' 3 ', ' spade') < Card(' 8 ', ' Diamond')
True
>>> Card(' 3 ', ' spade') > Card(' 8 ', ' Diamond')
False
>>> Card(' 3 ', ' spade') >= Card(' 8 ', ' Diamond ')
True
>>> Card(' 3 ', ' spade ') <= Card(' 8 ', ' Diamond ')
False
Class Hand) Also have to Implement to class Hand that represents a hand of playing cards. The class should have a constructor that takes as input the player ID (a string). It should support method addCard() that takes a card as input and adds it to the hand and method showHand() that displays the player’s hand in the format shown.
>>> hand = Hand( ' House ')
>>> deck = Deck()
>>> deck.shuffle()
>>> hand.addCard(deck.dealCard())
>>> hand.addCard(deck.dealCard())
>>> hand.addCard(deck.dealCard())
>>> hand.showHand()
House: 10 heart 8 spade 2 spade
>>> print(hand)
House: 10 8 2
Note that the last two outputs ( from__str__() and showhand() ) are the same.
*You may use the class Deck included in the slides.
Explanation / Answer
Deck is not provided so can't test ful program.
class Card:
def __init__(self, rank, suite):
self.rank = rank.strip()
self.suite = suite.strip()
def __lt__(self,other):
return int(self.rank) < int(other.rank)
def __le__(self,other):
return int(self.rank) <= int(other.rank)
def __gt__(self,other):
return int(self.rank) > int(other.rank)
def __ge__(self,other):
return int(self.rank) >= int(other.rank)
def __eq__(self,other):
return int(self.rank) == int(other.rank)
def __ne__(self,other):
return not(self.__eq__)
def __str__(self):
return self.rank + " " + self.suite
class Hand:
def __init__(self, id):
self .id = id.strip()
self.cards = []
def addCard(self, card):
if not card in self.cards:
self.cards.append(card)
def showHand(self):
print(self)
def __str__(self):
result = self.id + ": "
for card in self.cards:
result += str(card) + " "
return result
# Code link : https://paste.ee/p/q0qbg
Please note am print Heart and not its symbol.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.