Question

Write a function that flips a coin; it returns heads or tails. Use that function to...

Write a function that flips a coin; it returns heads or tails. Use that function to write a GetHeadsInARow functions that accepts the number of heads to roll in a row before you return with total rolls it took to get that number of heads in a row.

Please Code in Python3

my attempt below will not print out the answer and I do not know why

def HeadTails():
from random import randrange
"""Will simulate the flip of a coin."""
x = randrange(0,2)
if x == 0:
return 'tails'
else:
return 'heads'

def GetHeadsInARow():
user = input("Enter how many times you want heads in a row: ")
user_heads = int(user)
heads = 0
trys = 0
while heads < user_heads:
if HeadTails() == 'heads':
heads += 1
trys += 1
else:
heads = 0
trys += 1
return heads, trys
print("You got %d heads and took %d trys" %(heads, trys))

GetHeadsInARow()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE IN PYTHON:

from random import randrange
def HeadTails():
    x = randrange(0,2)
    if x == 0:
        return 'tails'
    else:
        return 'heads'

def GetHeadsInARow():
    user = input("Enter how many times you want heads in a row: ")
    user_heads = int(user)
    heads = 0
    trys = 0
    while heads < user_heads:
        if HeadTails() == 'heads':
            heads += 1
            trys += 1
        else:
            heads = 0
            trys += 1
    return heads, trys

heads, trys =GetHeadsInARow()
print("You got %d heads and took %d trys" %(heads, trys))

INDENTATION:

Please Indent the code as above.

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a function that flips a coin; it returns heads or tails. Use that function to...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me put this in order!! THANKYOU!! (CO 4 and 6) Create a program with...

    Please help me put this in order!! THANKYOU!! (CO 4 and 6) Create a program with a function named flipCoin that will flip a coin as many times as the user asks. The flipCoin function should return “heads” or “tails”. The program should continue while the user presses Y. SAMPLE OUTPUT: How many times do you want to flip a coin? 5 You got: tails You got: tails You got: tails You got: tails You got: heads To continue press...

  • Practice: • function • loops • if condition • random Roll a dice many times, you...

    Practice: • function • loops • if condition • random Roll a dice many times, you will see that each face value will count of roughly 1/6 • write a function • roll a dice once, return value will be 1-6, • use your functin I call your function 6000 times • count how many times of face 1 • count how many times of face 2 • print out the numbers of total face 1, 2, ..., 6 you...

  • Simulate the flipping of a coin Print the result of the coin flip : use off...

    Simulate the flipping of a coin Print the result of the coin flip : use off numbers for Heads . and even numbers for Tails Update the number of times the result is Heads Update the number of times the coin has been flipped struct coin_prob{ int heads; int flips; }; void coin_flip(struct coin_prob * coin); You may use a rand functioon (assume its already been seeded), but no other pre-defined function

  • Flip a coin 10 times and record the observed number of heads and tails. For example,...

    Flip a coin 10 times and record the observed number of heads and tails. For example, with 10 flips one might get 6 heads and 4 tails. Now, flip the coin another 20 times (so 30 times in total) and again, record the observed number of heads and tails. Finally, flip the coin another 70 times (so 100 times in total) and record your results again. We would expect that the distribution of heads and tails to be 50/50. How...

  • Python 3 code Write a function named coinflip that accepts an input called flips. The function...

    Python 3 code Write a function named coinflip that accepts an input called flips. The function should sim-ulate flipping a coin flips times. The function should return True if the coin was the same result for every flip. You can assume the function receives a positive integer as input. Write a second function named simulation that runs trials of the coin flip experiment. simulation should accept two parameters: the number of trials, and the number of coin flips to do...

  • "Flip a Coin!" You will write a program that allows the user to simulate a coin...

    "Flip a Coin!" You will write a program that allows the user to simulate a coin flip. Note the following line of code: (Math.random()*2) +1; This line generates a random number (a float) between 1 and 2. If you wanted to simulate somebody rolling a die, you could store the value of this variable as: var coinFlip = (Math.random()*2) +1; (Be sure to include all the parentheses in this line of code.) Reminder - Of course, you’ll need to convert...

  • You have a biased coin, where the probability of flipping a heads is 70%. You flip...

    You have a biased coin, where the probability of flipping a heads is 70%. You flip once, and the coin comes up tails. What is the expected number of flips from that point (so counting that as flip #0) until the number of heads flipped in total equals the number of tails?

  • You suspect that a coin is biased such that the probability heads is flipped (instead of...

    You suspect that a coin is biased such that the probability heads is flipped (instead of tails) is 52%. You flip the coin 51 times and observe that 31 of the coin flips are heads. The random variable you are investigating is defined as X = 1 for heads and X = 0 for tails, and you wish to perform a "Z-score" test to test the null hypothesis that H0: u = 0.52 vs. the alternative hypothesis Ha: u >  0.52....

  • You have a biased coin where heads come up with probability 2/3 and tails come up with probability 1/3. 2. Assume that you flip the coin until you get three heads or one tail. (a) Draw the possibilit...

    You have a biased coin where heads come up with probability 2/3 and tails come up with probability 1/3. 2. Assume that you flip the coin until you get three heads or one tail. (a) Draw the possibility tree. (b) What is the average number of flips? Use the possibility tree, and show your calculation. 2. Assume that you flip the coin until you get three heads or one tail. (a) Draw the possibility tree. (b) What is the average...

  • Random Number Generation and Static Methods Write a program that simulates the flipping of a coin...

    Random Number Generation and Static Methods Write a program that simulates the flipping of a coin n-times to see how often it comes up heads or tails. Ask the user to type in the number of flips (n) and print out the number of times the coin lands on head and tail. Use the method ‘random()’ from the Math class to generate the random numbers, and assume that a number less than 0.5 represents a tail, and a number bigger...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT