Tell us what’s happening:
I am unable to understand why I cannot pass the third test : “The draw method should behave correctly when the number of balls to extract is bigger than the number of balls in the hat.” All other tests, including the one that requires experiment method to return a different probability each time, have been passed. Can anyone understand why the third test fails
Your code so far
import copy
import random
class Hat:
def __init__(self, **balls):
self.contents = []
for k, v in balls.items():
self.contents.extend([k] * v)
def draw(self, num_to_draw):
num_of_balls = len(self.contents)
#list to store drawn balls
drawn_balls = []
if num_to_draw < len(self.contents):
for _ in range(num_to_draw):
to_remove = random.choice(self.contents)
drawn_balls.append(to_remove)
self.contents.remove(to_remove)
return drawn_balls
else:
return self.contents
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
M = 0
N = num_experiments
for _ in range(N):
trial_hat = copy.deepcopy(hat)
result = trial_hat.draw(num_balls_drawn)
drawn = {}
for i in result:
if i in drawn:
drawn[i] += 1
else:
drawn[i] = 1
fav_events = True
for ball, count in expected_balls.items():
if drawn.get(ball, 0) < count:
fav_events = False
break
if fav_events:
M +=1
return M/N
hat = Hat(black=6, red=4, green=3)
probability = experiment(hat=hat,
expected_balls={"red":2,"green":1},
num_balls_drawn=5,
num_experiments=2000)
print(probability)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Challenge Information:
Build a Probability Calculator Project - Build a Probability Calculator Project