Scientific Computing with Python Projects - Probability Calculator - Test 3

Tell us what’s happening:
Hi! I’ve been stuck in this test for a couple of days and can’t find the solution.

FAIL: test_prob_experiment (test_module.UnitTests)
Traceback (most recent call last):
  File "/home/runner/boilerplate-probability-calculator/", line 27, in test_prob_experiment
    self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
AssertionError: 0.262 != 0.272 within 0.01 delta (0.010000000000000009 difference) : Expected experiment method to return a different probability.

Ran 3 tests in 0.112s

FAILED (failures=1)

Your code so far
Link to replit

import copy
import random
# Consider using the modules imported above.

class Hat:
    def __init__(self, **kwargs):
        #The arguments passed into the hat are converted into a list of strings containing one item for each ball in the hat
        self.contents = []
        for k, v in kwargs.items():
            for val in range(v):

    def draw(self, k):
        #This method remove balls at random from contents and return those balls as a list of strings. The balls do not go back into the hat during the draw. If the number of balls to draw exceeds the available quantity, return all the balls.
        if k > len(self.contents):
            return self.contents
            contents_sample = random.sample(self.contents, k)
            for i in range(len(contents_sample)):
            return random.sample(self.contents, k)

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    # Method to determine the probability of getting the exact group of balls(expected_balls) from the hat(hat), drawing out of the hat in each experiment a determined number of balls(num_balls_drawn) performed an indicated number of time performed(num_experiments)
    m = 0 #Counts the number of time the expected balls are drawn out.
    for i in range(num_experiments):
        hat_copy = copy.deepcopy(hat)
        expected_balls_copy = copy.deepcopy(expected_balls)
        hat_sample = hat_copy.draw(num_balls_drawn)

        # Controls the value of balls expected if all are drawn counts it
        for i in expected_balls:
            expected_balls_copy[i] -= hat_sample.count(i)
        if all(value <= 0 for value in expected_balls_copy.values()):
             m += 1
    return m/num_experiments

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

The old tests for this project would have given you a helpful error:

ERROR: test_prob_experiment_one_expected_ball (test_module.UnitTests)
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-sc-probability-calculator/", line 70, in test_prob_experiment_one_expected_ball
    probability = prob_calculator.experiment(
  File "/home/gray/src/work/fcc-sc-probability-calculator/", line 151, in experiment
    hat_sample = hat_copy.draw(num_balls_drawn)
  File "/home/gray/src/work/fcc-sc-probability-calculator/", line 140, in draw
    return random.sample(self.contents, k)
  File "/usr/lib/python3.9/", line 450, in sample
    raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative

referring to the second return in your code here

since that is the second time you’ve pulled elements out of self.contents and the ValueError is telling you that you are attempting to remove more elements than are actually present.

Without the old tests, you have to trace back to find why you are missing some positive trials (since the probability is low). Fixing the draw method does allow your code to pass.

I was guessing that my problem was in the draw function and thanks to your help i can solve it.
Thank you

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.