Scientific Computing with Python Projects - Probability Calculator

Tell us what’s happening:

Describe your issue in detail here.

Your code so far

import copy
import random

class Hat:
    def __init__(self, **contents):
        self.contents = [flattened for sublist in (((key.split(' '))*value) for key, value in contents.items()) for flattened in sublist]
        self.contents_copy = copy.deepcopy(self.contents)

    # draw the ball from hat & update the list of the hat
    def draw(self, no_draw):
        if no_draw > len(self.contents):
            self.contents = copy.deepcopy(self.contents_copy)
        # randomly choosen ball based on no of draw
        drawn_ball = random.sample(self.contents, no_draw)
        
        # update list after ball is drawn 
        count = 0
        for draw in drawn_ball:
            count = 0
            for ball in self.contents:
                if count < 1:
                    if ball == draw:
                        self.contents.remove(ball)
                        count += 1
                else:
                    continue
        return drawn_ball

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    total = 0
    for _ in range(num_experiments):
        ball_drawn = hat.draw(num_balls_drawn)
        
        # turn ball_drawn list into dictionaries
        ball_drawn_dict = {}
        for x in ball_drawn:
            if x in ball_drawn_dict:
                ball_drawn_dict[x] += 1
            else:
                ball_drawn_dict[x] = 1
        
        total += counting(expected_balls, ball_drawn_dict)
    return total / num_experiments
                
def counting(expected_ball, ball_drawn_dict):
    count = 0
    if all(key in ball_drawn_dict and ball_drawn_dict[key] == value for key, value in expected_ball.items()):
        count += 1
    return count

i didn’t pass test 3, can you check what’s wrong.

Your browser information:

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

Challenge Information:

Scientific Computing with Python Projects - Probability Calculator

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.
Learning to describe problems is hard, but it is an important part of learning how to code.
Also, the more you say, the more we can help!

i didn’t pass test 3, can you check what’s wrong. When i test in vscode everything is ok