python help
so I'm trying to find this function
Write a function that estimates the probability of getting M or more heads out of N fair coin flips (by fair we mean the coin has equal chance of turning up heads or tails), repeating this sequence of flips a total of nTrials times. To perform this experiment, you should define a helper function coinFlipTrial(M, N) that performs one sequence of N coin flips and returns True if the number of heads is greater than or equal to M, and False otherwise. A test you can use for your functions is to estimate the probability of getting 8 or more heads out of 10 coin flips, which should be around 5 percent (0.05).
What i got:
import random
lst = ['Heads', 'Tails']
def coinFlipTrial(M, N):
count = 0
for i in range(0, N+1):
random.choice(lst)
if 'Heads':
count += 1
if count >= M:
return 'True'
else:
return 'False'
def coinFlipExperiment(M, N, nTrials):
count = 0
for i in range(1, nTrials +1):
coinFlipTrial(M, N)
if 'True':
count += 1
return (0.5**(N-(M-1)))*count
I'm not getting the correct returns though
What's the error in my code?
Edit: nvm I got it
4 Comments • Newest first
[quote=GizzyJones]Just so you know, if you're going to pursue a career in CS, you should learn to form your questions better. People don't take kindly to questions like "what's wrong? fix it please". Instructors and such will generally be more inclined to help you if you ask questions that show you've thought about what you've written, understand it, and that you're just stuck, etc. Rather than "what's wrong?" you could show that certain parts are what's expected (maybe test your output or results up to a certain point of your code, though this seems kind of like a beginner class so there's probably not much you can really test).
A way you could test would be to somehow print out output every time you go through your for loop and track it as it goes through. Stuff to that extent. It helps you understand what's actually happening and maybe even where/why you're going wrong.[/quote]
alright will do thanks
Just so you know, if you're going to pursue a career in CS, you should learn to form your questions better. People don't take kindly to questions like "what's wrong? fix it please". Instructors and such will generally be more inclined to help you if you ask questions that show you've thought about what you've written, understand it, and that you're just stuck, etc. Rather than "what's wrong?" you could show that certain parts are what's expected (maybe test your output or results up to a certain point of your code, though this seems kind of like a beginner class so there's probably not much you can really test).
A way you could test would be to somehow print out output every time you go through your for loop and track it as it goes through. Stuff to that extent. It helps you understand what's actually happening and maybe even where/why you're going wrong.
[quote=xoqtprincessxo]Well first of all, you're iterating N+1 times instead of N times in coinFlipTrial(M,N)
range(1,N+1) or range(0,N) will prob work better[/quote]
ah right thanks for that
Well first of all, you're iterating N+1 times instead of N times in coinFlipTrial(M,N)
range(1,N+1) or range(0,N) will prob work better