I almost finished the task but in the experiment function i can’t put list with draw balls inside for loop to get everytime new result. (i got value error)
import copy
import random
# Consider using the modules imported above.
class Hat:
def __init__(self, **args):
self.contents = list()
for v,k in args.items():#n-times add color ball
self.contents += k*[v]
def draw(self, num_balls):
if(num_balls > len(self.contents)):
return self.contents
else:
drawballs = list()
for k in range(num_balls):
drawballs.append(self.contents.pop(random.randint(0, len(self.contents)- 1 - k)))
return drawballs
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
expect = list()
balls = copy.deepcopy(hat)
allballs = balls.draw(num_balls_drawn)
for v,k in expected_balls.items():
expect += k * [v]
m = 0
for i in range(num_experiments):
#allballs = balls.draw(num_balls_drawn)
if(all(x in allballs for x in expect)):
m += 1
print(m)
prob = m / num_experiments
return prob