The possibility of a coin landing on its side. Print("The longest streak of heads was " + str(max(trial)) +". Usually, coins used in probability problems are only assumed to have two outcomes: heads or tails. Temp+=1 # adding one to temporary streak if it is a headsĮlse: # this block executes if streak is broken Return random.choice() # using random.choice() Though the logic isn't wrong, you need not make the list 1st and then check streak, you can simply check it together, it will take less time and space.Īlso, your logic is correct, but this one would be better: import random Print("The longest streak of heads was " + str(max(trial)) +".") Temp2 = simulate(100) # SImulate 10000 coin flipsĬoinFlip = countStreak(temp2) # Check streak of variable trial Also, you need not check the streak every time so the check_streak() need to be out of the loop and we need to pass the result obtained from simulate(10000) into it.Ĭoin = random.randint(0, 1) # better option would be to use random.choice()įor i in range(num): # this needs to run num times But actually, you had to run it once, but return a list of 10000 items. So there was a flaw in your code, you were running simulate() function 10000 times. Print("The longest streak of heads was " + str(coinFlip) +".") # need something here to track count of streaks for "H" Keep track of the current largest streak of heads and display this result after the test loop completes. Coin flips are sometimes even used to determine which sports team will start with possession of the ball, which can give a significant advantage to one team or the other. Given a choice between two options, some people turn to the flip of a coin to tell them which option to choose. In the main function, write a testing loop that simulates the process 10000 times. A coin flip has long been used as an impartial determiner. Great for young kids games to pick a color easily or even a match. Choose from Yellow / Black or Red / Black. If you see a tail, check if the current streak of heads is larger than your current longest streak. Simplify your coin toss with the b+d coin. Keep track of the current number of heads and the current largest streak of heads in two separate variables.Īs you iterate through the list, keep track of the current number of heads you've seen in a row. This function returns a list of length numFlips containing H's and T's.ĭef countStreak(flips_list) - iterates through the flips list passed to it and counts streaks of 'H's and returns the largest streak it finds. I also don't know if my program is calculating the streaks correctly.ĭef flipCoin() - returns 'H' or 'T' with the same probability as a coin.ĭef simulate(numFlips) - simulates flipping a coin numFlips(100) times. I am stuck on how to finish the def main() function, specifically the counter. I need to create a python program that will use various functions to simulate flipping a coin 100 times and finding the largest streak of "H"'s out of 10,000 tries.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |