So I’m failing the test_prob_experiment test. I should be getting 0.272 with this code but I’m getting 0.252 instead. Can someone explain to me what those numbers mean and give me a hint on how to fix the code?
this is my code
import copy
import random
# Consider using the modules imported above.
class Hat:
def __init__(self, **balls):
contents = []
self.contents = contents
self.balls = balls
for key, value in self.balls.items():
for i in range(value):
contents.append(key)
def draw(self, number_draws):
drawn_balls = []
if number_draws > len(self.contents):
drawn_balls.extend(self.contents)
self.contents.clear
return drawn_balls
else:
for i in range(number_draws):
random.shuffle(self.contents)
removed_ball = self.contents.pop()
drawn_balls.append(removed_ball)
return drawn_balls
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
M = 0
for i in range(num_experiments):
expected_balls_copy = copy.deepcopy(expected_balls)
hat_copy = copy.deepcopy(hat)
drawn_items= hat_copy.draw(num_balls_drawn)
for color in drawn_items:
if color in expected_balls_copy:
expected_balls_copy[color] -= 1
if all(x <= 0 for x in expected_balls_copy.values()):
M +=1
probability = M / num_experiments
return probability
and this is the output
python main.py
Probability: 0.189
..F
======================================================================
FAIL: test_prob_experiment (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-probability-calculator-1/test_module.py", line 26, in test_prob_experiment
self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
AssertionError: 0.252 != 0.272 within 0.01 delta (0.020000000000000018 difference) : Expected experiment method to return a different probability.
----------------------------------------------------------------------
Ran 3 tests in 0.717s
FAILED (failures=1)