Question

With the code given write python code that prints the probability of getting a flush when...

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

Homework Answers

Answer #1

-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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Probabilities with a deck of cards. There are 52 cards in a standard deck of cards....
Probabilities with a deck of cards. There are 52 cards in a standard deck of cards. There are 4 suits (Clubs, Hearts, Diamonds, and Spades) and there are 13 cards in each suit. Clubs/Spades are black, Hearts/Diamonds are red. There are 12 face cards. Face cards are those with a Jack (J), King (K), or Queen (Q) on them. For this question, we will consider the Ace (A) card to be a number card (i.e., number 1). Then for each...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
he following question involves a standard deck of 52 playing cards. In such a deck of...
he following question involves a standard deck of 52 playing cards. In such a deck of cards there are four suits of 13 cards each. The four suits are: hearts, diamonds, clubs, and spades. The 26 cards included in hearts and diamonds are red. The 26 cards included in clubs and spades are black. The 13 cards in each suit are: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. This means there are four...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
The following question involves a standard deck of 52 playing cards. In such a deck of...
The following question involves a standard deck of 52 playing cards. In such a deck of cards there are four suits of 13 cards each. The four suits are: hearts, diamonds, clubs, and spades. The 26 cards included in hearts and diamonds are red. The 26 cards included in clubs and spades are black. The 13 cards in each suit are: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. This means there are four...
As shown above, a classic deck of cards is made up of 52 cards, 26 are...
As shown above, a classic deck of cards is made up of 52 cards, 26 are black, 26 are red. Each color is split into two suits of 13 cards each (clubs and spades are black and hearts and diamonds are red). Each suit is split into 13 individual cards (Ace, 2-10, Jack, Queen, and King). If you select a card at random, what is the probability of getting: 1) A(n) 8 of Heart s? 2) A Club or Spade?...
Part A Poker Hands: In this activity, we will apply some of the various counting techniques...
Part A Poker Hands: In this activity, we will apply some of the various counting techniques that we have studied including the product and sum rules, the principle of inclusion-exclusion, permutations, and combinations. Our application will be counting the number of ways to be dealt various hands in poker, and analyzing the results. First, if you are not familiar with poker the following is some basic information.   These are the possible 5-card hands: Royal Flush (A,K,Q,J,10 of the same suit);...
home / study / math / statistics and probability / statistics and probability questions and answers...
home / study / math / statistics and probability / statistics and probability questions and answers / The Following Question Involves A Standard Deck Of 52 Playing Cards. In Such A Deck Of Cards ... Question: The following question involves a standard deck of 52 playing cards. In such a deck of cards ther... (2 bookmarks) The following question involves a standard deck of 52 playing cards. In such a deck of cards there are four suits of 13 cards...
As shown above, a classic deck of cards is made up of 52 cards, 26 are...
As shown above, a classic deck of cards is made up of 52 cards, 26 are black, 26 are red. Each color is split into two suits of 13 cards each (clubs and spades are black and hearts and diamonds are red). Each suit is split into 13 individual cards (Ace, 2-10, Jack, Queen, and King). If you select a card at random, what is the probability of getting: (Round to 4 decimal places where possible) a) A 9 of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT