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!

11th Jan 2020, 5:31 PM
Priel
Priel - avatar
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))
11th Jan 2020, 5:36 PM
Russ
Russ - avatar
+ 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.
11th Jan 2020, 6:01 PM
Russ
Russ - avatar
+ 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)
11th Jan 2020, 5:42 PM
Priel
Priel - avatar
0
OH! thank you!
11th Jan 2020, 6:02 PM
Priel
Priel - avatar