Question: coin_flip Language/Type: Python parameters Random Write a function named coin_flip that simulates repeatedly flipping a two-sided coin until a particular side (Heads or Tails) comes

coin_flip

Language/Type: Python parameters Random

Write a function named coin_flip that simulates repeatedly flipping a two-sided coin until a particular side (Heads or Tails) comes up several times consecutively (in a row). Your function accepts two parameters, an integer k and a character side, where side is expected to be 'H' for Heads or 'T' for Tails. You should keep simulating the flipping of the coin until k occurrences of the given side are seen consecutively. For example, if the call is coin_flip(3, 'H') , you should flip the coin until Heads is seen 3 times in a row. Here is an example output from the call of coin_flip(4, 'T')

T H T H T T H T T H H T H H H H H T T T T
You got T 4 times in a row!

If a negative k is passed, and/or a side value is passed that is not 'H' or 'T', your function should prERROR! and exit immediately.

Use random or randint to give an equal chance to a head or a tail appearing. Each time the coin is flipped, what is seen is displayed (H for heads, T for tails), separated by spaces. When k consecutive occurrences of the given side occur, a congratulatory message is printed. Match our output format exactly.

It will be tested with bellow inputs:

coin_flip(3, "H") output H H H

coin_flip(4, "T") output T H H T H H H H T T T T

coin_flip(1, "H") output T T H

coin_flip(0, "T") My cod does not pass this part

coin_flip(-1, "H") output Error

coin_flip(6, "x") Output Error

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 Databases Questions!