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. Mar 22 31 views

## 1 answer to this question.

Refer to the code below

```import random
toss = []
for j in range(100):
toss.append(random.randint(0, 1))
for j in range(len(toss)):
if toss[j] == 0:
toss[j] = "h"
else:
toss[j] = "t"

print("Probablity Tails:", str(round((toss.count('t')/len(toss))*100)) + "%")``` answered Mar 23 by
• 6,000 points

## Transition matrix to have percentages instead of default probability - Markov chain

While using Markov chain, is it possible ...READ MORE

## Plotting joint probability of two random variable choices

Create pandas.DataFrame , you can use seaborn.jointplot ...READ MORE

## Understanding the probability of a double-six if i roll two dice

The chance of not getting a double ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE