Question

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 each trial. Return the percentage of trials that all the flips had the same result

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

Please refer to code screenshots for better understanding of indentation

Program Screenshots

Output

Program code to copy

# randint function imported to generate random value from 0 and 1
from random import randint

def coinflip(flips):
  
   # count of heads and tails
   head = 0
   tail = 0
  
   # flipping coin flips times
   for i in range(flips):
       flip = randint(0, 1) # flipping coin
      
       #if flip is 0 then head count is increase otherwise tails count is increased
       if flip == 0:
           head += 1
       else:
           tail += 1
  
   # if all heads or all tails   return True
   if head == flips or tail == flips:
       return True
  
   return False
  
def simulation(trials, flips):
  
   # count stores number of trial which had all flips had same value
   count = 0
  
   # doing trials trials times
   for trial in range(trials):
       count += int(coinflip(flips))
  
   # calculating percentage
   percent = (count / trials) * 100
  
   return percent
  

#d driver function to show output
def main():
   trials = 23
   flips = 7
   percent = simulation(trials, flips)
   print("\nPercentage of trials that all the flips had same result for %d trials and %d flips in each trial is %.2f\n" % (trials, flips, percent))


main()

Add a comment
Know the answer?
Add Answer to:
Python 3 code Write a function named coinflip that accepts an input called flips. The function...
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
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