Q: Help in being able to use this example on shell to execute this program. I am
ID: 3830810 • Letter: Q
Question
Q: Help in being able to use this example on shell to execute this program. I am missing to add hand (I think it has to be an attribute), do not know how:
##Example:
##CardHand = Hand('Player_ID')
##CardHand.addcard("Two of Diamonds")
##CardHand.addcard("Five of Spades")
##CardHand.addcard("Ace of Hearts")
##CardHand.showHand()
def main():
c1 = Card('3','u2660')
c2 = Card('4','u2661')
print('***** card 1 *****')
print(c1.getRank(),c1.getSuit())
print('***** card 2 *****')
print(c2.getRank(),c2.getSuit())
print('***** Now printing the cards using __str__ *****')
print(c1)
print(c2)
class Card:
'represents a playing card'
def __init__(self, rank, suit):
'initialize rank and suit of card'
self.__rank = rank
self.__suit = suit
def getRank(self):
'return rank'
return self.__rank
def getSuit(self):
'return suit'
return self.__suit
def __str__(self):
return '{},{}'.format(self.__rank, self.__suit)
'overload the < operator, in python internally its lt'
def __lt__(self,other):
' get the rank of self object'
self_rank = self.getRank()
' get the rank of the object that is being compared'
other_rank = other.getRank()
' return the result'
return self_rank < other_rank
'overload the > operator, in python internally its gt'
def __gt__(self,other):
' get the rank of self object'
self_rank = self.getRank()
other_rank = other.getRank()
return self_rank > other_rank
'overload the == operator, in python internally its eq'
def __eq__(self,other):
' similar as above'
self_rank = self.getRank()
other_rank = other.getRank()
return self_rank == other_rank
import itertools, random
class Hand:
playerID = "" #two data members- one, a string and the other an empty list which will hold strings, signifyinh the hand
cards = []
def __init__(self,ID): #constructor, a string is passed to this via ID and is assigned to the data member 'PlayerID'
self.playerID = ID
def addCard(self,card): #function addcard() accepts a string,'card' as a parameter and appends it to the list
self.cards.append(card)
def showHand(self): #show the current hand by iterating through the list
for i in range(len(self.cards)):
print(self.cards[i])
class Deck:
'represents a deck of 52 cards'
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?
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
Is something is not working comment and I wil resolve.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.