Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

Hi, really unsure why the final test case isnt passing. The probability values im getting are roughly around the same range (0.374,0.3768,0.354 etc etc). What exactly is expected especially since it says every test result is different too?

Your code so far


def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    success=0
    for i in range(num_experiments):
        drawn=hat.draw(num_balls_drawn)
        correct=True
        for j in expected_balls:
            if expected_balls[j]>drawn.count(j):
                correct=False
        if correct:
            success+=1
        hat.contents.extend(drawn)
    return success/num_experiments

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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

Please show all of your code

import copy
import random

class Hat:
    def __init__(self,**kwargs):
        self.contents=[]
        for i in kwargs:
            self.contents.extend([i]*kwargs[i])
    def draw(self,num):
        if num>len(self.contents):
            removed=self.contents.copy()
            self.contents=[]
        else:
            removed=[]
            for i in range(num):
                removing=random.choice(self.contents)
                removed.append(removing)
                self.contents.remove(removing)
        return removed

 
        

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    success=0
    for i in range(num_experiments):
        drawn=hat.draw(num_balls_drawn)
        correct=True
        for j in expected_balls:
            if expected_balls[j]>drawn.count(j):
                correct=False
        if correct:
            success+=1
        hat.contents.extend(drawn)
    return success/num_experiments