Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

Could someone please help me figure out how to troubleshoot this error message?

AssertionError: 0.261 != 0.272 within 0.01 delta (0.01100000000000001 difference) : Expected experiment method to return a different probability.

Any tips or advice would be greatly appreciated. Thanks a bunch!

Best regards,
RR

Your code so far

import copy
import random

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    experiments_num = 0
    hat_contents = copy.copy(hat.contents)
    sgrawk = {
        ball: hat_contents.count(ball)
        for ball in set(hat_contents)
    }

    for _ in range(num_experiments):
        hat_copy = Hat(**sgrawk)
        balls_drawn = hat_copy.draw(num_balls_drawn)

        drawn_counts = {
            ball_drawn: balls_drawn.count(ball_drawn)
            for ball_drawn in set(balls_drawn)
        }

        success = all(
            drawn_counts.get(color, 0) >= count
            for color, count in expected_balls.items()
        )

        if success:
            experiments_num += 1

    return experiments_num / num_experiments

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) 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

provide also your Hat class, it’s necessary for debugging

Thank you. Here is the Hat class:

class Hat:
    def __init__(self, **kwargs):
        self.contents = [
            ball_color
            for ball_color, ball_number in kwargs.items()
            for _ in range(ball_number)
        ]

    def draw(self, num_balls_drawn):
        if num_balls_drawn >= len(self.contents):
            balls_drawn = self.contents
            self.contents = []
        else:
            balls_drawn = random.sample(self.contents, num_balls_drawn)
            for ball in balls_drawn:
                self.contents.remove(ball)

        return balls_drawn

Hi, I passed the test without changing a thing! It feels great knowing my approach works now. Thanks!

1 Like

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