When using this loop, I'm not getting the results I want and I'm not sure how to get the code to work on Python

StackOverflow https://stackoverflow.com/questions/23307715

  •  09-07-2023
  •  | 
  •  

Question

I'm trying to make a heads and tails result table, with each time the "coin" is "flipped" from a range of 1-10, 1-100, 1-1,000, 1-10,000, and 1-100,000.But my results after the first toss are mixed.

import random

print("heads  tails")
resultList=[0,0]
start=10
maximum=[]

for exponent in range (1,6):
    ranges=start**exponent   
     for num in range(1,ranges+1):
        result=random.randint(0,num)
        if result%2==0:
            resultList[0]+=1
        elif result%2 != 0:
            resultList[1]+=1
    print(resultList)

But my output is coming out good for the first toss then it all goes downhill.

heads  tails
[3, 7]
[49, 61]
[566, 544]
[5533, 5577]
[55165, 55945]

Any recommendations in what I'm doing wrong? Something tells me it's something simple I am overlooking.

Was it helpful?

Solution

You are not clearing resultList after the previous iteration. Just move resultList=[0,0] inside the for loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top