0
Probability
Hi! so iv got a little probability expirement and iv got a error in this code: import random for i in range(20): ran = random.randint(1,6) ran1 = random.randint(1,6) print(ran + ran1) import re pattern = 7 print(len(re.findall(pattern,i))) ---------------------------------------- in the code i want to print the number of times the number 7 apears in the random numbers range but it do not work... PLEAS HELP ME!
4 Answers
+ 3
My advice would to append the sum (ran + ran1) to a list (call it my_list for example) and then do my_list.count(7) at the end.
import random
my_list = []
for i in range(20):
ran = random.randint(1,6)
ran1 = random.randint(1,6)
my_list.append(ran + ran1)
print(my_list.count(7))
+ 2
The problem there is that, with each iteration, you are creating a new empty list called 'test'. What you want is a list to be created before the for loop.
+ 1
Do you mean like this?:
import random
for i in range(100):
ran = random.randint(1,6)
ran1 = random.randint(1,6)
test = []
test.append(ran + ran1)
result = test.count(7)
print(result)
0
OH!
thank you!