Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using python! Help! For this project, you implement to classes with the followin

ID: 3834149 • Letter: U

Question

Using python! Help! For this project, you implement to classes with the following specifications. The two classes must be placed in a single file called 'project3_solution.py' and submitted on Mimir. Class Card) Overload appropriate operators for class Card so that you can compare cards based on rank: >>> Card('3', ' ') >> Card('3', ' ') >> Card('3', ' ') >> Card('3', ' ') >> hand = Hand ('House') >>> deck = Deck() >>> deck.shuffle() >>> hand.addCard(deck. dealCard()) >>> hand.addCard(deck.dealCard()) >>> hand.showHand() House: 10 8 2 >>> 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

import random

class Card:
def __init__(self, rank, suite):
rank = rank.strip()
self._rank = rank
self.rank = rank
if rank == 'A':
self.rank = 14
elif rank == 'J':
self.rank = 11
elif rank == 'Q':
self.rank = 12
elif rank == 'K':
self.rank = 13
else:
self.rank = int(rank)
  
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 Deck:
'represents a deck of 52 cards'
# ranks and suits are Deck class variables
ranks = {'2','3','4','5','6','7','8','9','10','J','Q','K','A'}
# suits is a set of 4 Unicode symbols representing the 4 suits
suits = {'u2660', 'u2661', 'u2662', 'u2663'}
def __init__(self):
'initialize deck of 52 cards'
self.deck = [] # deck is initially empty
for suit in Deck.suits: # suits and ranks are Deck
for rank in Deck.ranks: # class variables
# add Card with given rank and suit to deck
self.deck.append(Card(rank,suit))

def dealCard(self):
'deal (pop and return) card from the top of the deck'
return self.deck.pop()
def shuffle(self):
'shuffle the deck'
random.shuffle(self.deck) #not doing anything; should randomize a list?

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

hand = Hand( ' House ')
deck = Deck()
deck.shuffle()
hand.addCard(deck.dealCard())
hand.addCard(deck.dealCard())
hand.addCard(deck.dealCard())
hand.showHand()
print(hand)

# code link: https://paste.ee/p/1q9FI

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote