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
  • python In a program, write a function named roll that accepts an integer argument number_of_throws. The...

    python In a program, write a function named roll that accepts an integer argument number_of_throws. The function should generate and return a sorted list of number_of_throws random numbers between 1 and 6. The program should prompt the user to enter a positive integer that is sent to the function, and then print the returned list.

  • Python problem QUESTION 48 Write a function named was that accepts two integer values as arquments...

    Python problem QUESTION 48 Write a function named was that accepts two integer values as arquments and returns the wake that is greater of the two for example, if 7 and 12 are passed as arguments the function should return 12. If the arguments are equal the function should return zero. TTTA 3112) T V 2

  • Write a program in Python that accepts as input an integer N and a real number...

    Write a program in Python that accepts as input an integer N and a real number c, and outputs the coefficient of the Nth degree term of the Taylor series for the function f(x) = ex centered at c. This process should recur until the user enters a quit code. Erroneous input should be excepted as necessary, and should result in a new prompt.

  • Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts...

    Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts a single variable as input i Create a row vector Tthat represents the number of seconds in two minutes, starting ii. Call the function optimist on the vector T, store the result in variable R1 and returns the double of that variable from one and ending at a hundred and twenty v. Create an anonymous function named pessimist, that accepts a single variable as...

  • In Python: Write the definition for a function named avg which accepts two int parameters, and...

    In Python: Write the definition for a function named avg which accepts two int parameters, and returns the average of the two values Write a statement that calls this function to print out the average of values 11 and 25.

  • PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns...

    PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. Test case: the arguments are “apple@123” and “banana@#345”, your program should return “a@3”. Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. (20 pts) Test case: the arguments are "apple@123” and “banana@#345”, your program should return "a@3".

  • 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."""...

  • In python write a module named randStr.py that has 3 functions. Each function has an optional...

    In python write a module named randStr.py that has 3 functions. Each function has an optional second parameter to set a particular seed. 1) randWord accepts a string and will return a random word from that string (return value is string). 2) strMixer will randomly change words inside the string and returns a string -- If the string is only one word, then it will mix the letters of the word and return the mixed word as a string 3)...

  • IN C++ PLEASE Write a class LuckyNumberChecker. Include a constructor that accepts an integer named luckyDivisor...

    IN C++ PLEASE Write a class LuckyNumberChecker. Include a constructor that accepts an integer named luckyDivisor Add a virtual method named checkNumber that accept an integer named number and returns a string If the number argument is evenly divisible by the luckyDivisor, return "This number is lucky" otherwise, if the number is odd, return "This number might be lucky" otherwise, return "this number is not lucky" Write a class DoublyLuckyNumberChekcer that extends LuckyNumberChecker Add a constructor that accepts two integers,...

  • Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts...

    Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts one integer parameter that returns a list with the same number of terms as the value of the parameter. If the parameter is zero or a negative number, you should return an empty list A triangular number is the sum of a positive integer and all the integers before it. The series is as follows: 1, 3, 6, 10, 15, 21,

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