Question: write the code import random class RandomCard: ''' Class that creates objects that are random cards drawn from a deck with replacement ''' def


 

 write the code 

 


import random

class RandomCard: ''' Class that creates objects that are random cards drawn from a deck with replacement ''' def __init__(self): '''draw a card by pulling a random suit and value in that suit''' suits = (\"Hearts\",\"Diamonds\",\"Spades\",\"Clubs\") values = (\"Ace\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"Jack\",\"Queen\",\"King\") self.suit = random.choice(suits) self.value = random.choice(values)

def __str__(self): '''printing method''' return self.value+\" of \"+self.suit

def same_suit(self,other): '''checks if two cards have the same suit''' return self.suit == other.suit

def __eq__(self,other): '''checks if two cards are the same''' return (self.suit==other.suit) and (self.value==other.value)

class Hand: '''deal a hand of n cards''' def __init__(self,n): '''pull n cards at random''' self.cards = [] i = 1 while i => card = RandomCard() # make sure you don't add the same card twice if card not in self.cards: self.cards.append(card) i += 1

def __str__(self): '''print a hand; relies on str method from random_card class''' s = \"(\" for i in range(len(self.cards)): s += self.cards[i].__str__() if i s += ', ' else: s += \")\" return s

def flush(self): '''check for flush''' res = True for card in self.cards[1:]: # all cards must be the same suit as the first card if not(RandomCard.same_suit(self.cards[0],card)): res = False return res

# count the number of flushes in 100,000 hands num_flush = 0 for i in range(100000): h = Hand(5) if h.flush(): num_flush += 1

print(\"Frequency of flushes:\",num_flush / 100000)





Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!