Hey everyone, hope you are all having a good day.
So I’m working on the probability calculator, and I keep getting the same unittest error that my output doesn’t match the expected output:
FAIL: test_prob_experiment (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-probability-calculator/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.096 != 0.272 within 0.01 delta (0.17600000000000002 difference) : Expected experiment method to return a different probability.
----------------------------------------------------------------------
Ran 3 tests in 0.004s
FAILED (failures=1)
I know what the error is about, but I tried so many different ways and the probability still doesnt match. Here is the code I wrote that in my head makes the most sense:
def experiment(hat=None, expected_balls=None, num_balls_drawn=None, num_experiments=None):
hat_copy = copy.copy(hat)
match = 0
verify = []
# looping the amount of experiments
for i in range(num_experiments):
# getting random balls
balls = [random.choices(hat_copy.contents, k=num_balls_drawn)]
# loop through expected balls: ball represents each ball
for ball in expected_balls:
# getting value of each expected ball: the amount of times it is expected to show
value = expected_balls.get(ball)
# if the random balls list has at least the number of balls expected from a color
# append True to the verify list for later use, else append false
if balls[0].count(ball) >= value:
verify.append(True)
else:
verify.append(False)
# when the length of verify equals the length of expected balls, meaning that verify contains the result
# of the encounter of each ball in expected_balls (True or False)
if len(verify) == len(expected_balls):
# if all elements in verify equal True than add 1 to match, match stands for the number of times
# there is a match in expected_balls and the random balls
if all(verify):
match += 1
# empty verify so next experiment will identical
verify = []
# divide match (number of times there was a match) and the number of experiments realized
probability = match / num_experiments
return probability
I tried my best to explain the function with comments in the code, so you can understand.
I am really stuck becaue this seems correct to me, but maybe you can see something that I don’t.
Here is a link to all the code: https://replit.com/@SolGeller/boilerplate-probability-calculator#prob_calculator.py
Thanks a lot in advance!!!