I want to calculate the experimental probability of heads in coin toss by generating 0s or 1s randomly and assign 'h' for 0 and 't' for 1 to n. The number of flips is 100.
import random
array_ht = []
flip_count = 0
while flip_count != 100:
n = random.randint(0,1)
if n == 0:
n = "h"
elif n == 1:
n = "t"
flip_count += 1
array_ht.append(n)
for x in array_ht:
h_count = 0
if x == "h":
h_count += 1
print(h_count + "%")
The for loop looks through every object of array_ht and if it finds an "h", it adds 1 to the number of head flips. But the code isn't working and just prints "1% 0% 1% 0% 0% ..."
What should actually happen is for example if sequence generated was '0,1,1,0,0,0,1,1,1' then array_ht = [h,t,t,h,h,h,t,t,t] and probability to be printed is (4/9*100=) 44.444444%.
Actually we don't need to assign 'h' or 't' to n, we can just look for number of 0s but earlier I had wanted to print array_ht also.