With the code given write python code that prints the probability of getting a flush when you run 10**5 trails.
this is what i have so far but it says that isFlush is not defined. why?
# Print out probability that a 5-card hand has all the same suit
#seed(0)
num_trials = 10**5
trials = [dealHand for k in range(num_trials)] # 5 card
hand
prob = sum([l for h in trials if isFlush(h)])/num_trials # sum the
list of numbers that are a flush
def isFlush(h):
dist = counter([suit(c) for c in h])
if 5 in dist.values():
print('the probablity of a flush is ' + str(prob))
HELPER CODE:
# We will represent cards as a string, e.g., 'AC' will be Ace of Clubs
# Denominations: 2, ..., 10, 'J' = Jack, 'Q' = Queen, 'K' =
King, 'A' = Ace
Denominations =
['2','3','4','5','6','7','8','9','10','J','Q','K','A']
# Suits 'S' = Spades, 'H' = Hearts, 'D' = Diamonds, 'C' =
Clubs
Suits = ['C', 'H', 'S', 'D']
# Note that colors are determined by the suits (hearts and
diamonds are red, others black,
# so, AC is Black
# List comprehensions are a great way to avoid explicit for loops
when creating lists
Deck = [(d+s) for d in Denominations for s in Suits] # Note the double for loop
print( Deck )
# Now we can "deal" cards by choosing randomly from the deck
seed(0) # seed makes sure that all your computations start with
the same random sequence;
# this not really important, and only necessary for debugging and
grading.
def dealCard():
return choice(Deck) # choice randomly chooses an element of a
list
print( dealCard() )
# When dealing a hand in cards, the selection of cards is
without replacement, that is, cards are removed from
# the deck one by one and not put back. This can be simulated in
the choice function by setting the replace
# parameter to False.
seed(0)
def dealHand(withReplacement = False,size = 5):
return choice(Deck,size,withReplacement) # chooses a list of size
elements
print( dealHand() )
# extract the denomination and the suit from a card
def denom(c):
return c[0:-1]
def suit(c):
return c[-1]
# The function rank(c) will simply return the position of the
card c PLUS 2 in the list 2, 3, ...., K, A. This will be used in an
essential
# way in our code below. Although in the diagram given lecture, Ace
is below 2, the Ace is actually considered to be ordered
# above the King, for example in determining a straight, under "Ace
high rules."
# rank(2) = 2, ...., rank(10) = 10, rank(Jack) = 11, rank(Queen) = 12, rank(King) = 13, rank(Ace) = 14
def rank(c):
return Denominations.index(denom(c))+2
# Now we want to identify various kinds of cards
def isHeart(c):
return ( suit(c) == 'H')
def isDiamond(c):
return ( suit(c) == 'D')
def isClub(c):
return ( suit(c) == 'C')
def isSpade(c):
return ( suit(c) == 'S')
def isRed(c):
return ( isHeart(c) or isDiamond(c) )
def isBlack(c):
return (not isRed(c))
def isFaceCard(c):
return rank(c) >= 11 and rank(c) <= 13
-In Python you cannot use any function untill you define it otherwise it will give error as function is not define;
example::
In above Example call is used before it is defined so it gave error
-In your case,You are using isFlush before the isFlush define so you have to define the isFlush function before the you have used
Solution::
So you should define isFlush function before
prob = sum([l for h in trials if isFlush(h)])/num_trials # sum the list of numbers that are a flush
statement not after it
Get Answers For Free
Most questions answered within 1 hours.