Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

Please help. Having trouble with tests 2 and 4.
For test # 2 I tried printing the lengths of “contents” before and after the call to “draw’” and they do differ by the amount passed in to “draw”. So I’m unsure what’s wrong.
For test # 4 I do get a different probability from the example. I understand that in the experiment, for each trial in “num_experiments” 5 balls will be picked out of the 13 in the hat. Is this right or am I misinterpreting?

Your code so far

import copy
import random


class Hat:

    def __init__(self, **kwargs):
        if not kwargs:
            raise ValueError('Class object must contain at least one argument.')

        else:
            self.contents = [key for key, value in kwargs.items() for v in range(value)]

    def draw(self, draw_num):
        balls_withdrawn = []

        if not isinstance(draw_num, int):
            raise TypeError('The number of balls to draw should be an integer.')

        if draw_num >= len(self.contents):
            balls_withdrawn = copy.deepcopy(self.contents)
            self.contents.clear()
            return balls_withdrawn

        elif draw_num <= 0:
            raise ValueError('The number of balls to draw should be greater than 0.')

        else:
            while draw_num > 0:
                amount_in_hat = len(self.contents)
                x = random.randint(1, amount_in_hat - 1)
                y = self.contents.pop(x)
                balls_withdrawn.append(y)
                draw_num -= 1
            return balls_withdrawn


def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    success = 0
    counter = 0
    balls_for_exp = [key for key, value in expected_balls.items() for v in range(value)]

    while counter < num_experiments:
        doppelganger = copy.deepcopy(hat)
        t_or_f_list = []
        balls_picked = doppelganger.draw(num_balls_drawn)
        for i in range(len(balls_for_exp)):
            if balls_for_exp.count(balls_for_exp[i]) <= balls_picked.count(balls_for_exp[i]):
                t_or_f_list.append(True)
            else:
                t_or_f_list.append(False)

        if all(t_or_f_list):
            success += 1

        counter += 1

    p = success / num_experiments
    return p

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/140.0.0.0 Safari/537.36

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

you may need to try different methods, the tests expect a particular result, so a different method may be needed

doesn’t pop remove the item at the specified index? isn’t it that like this it can’t ever remove the first one at index 0?

1 Like

Thank you for the quick reply and your guidance. That was it.