Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

  1. The experiment method should return a different probability.

my probability is really close to the one they have but it says im still failing. Anyone have any advice?

Your code so far

import copy
import random

class Hat:
    
    def __init__(self, **kwargs):
        self.contents = []
        for (types, value) in kwargs.items():
            self.contents += value*[types]
    
    def draw(self,numberdrawn):
        taken = []

        if numberdrawn >= len(self.contents):
            taken += self.contents
            self.contents.clear()
            return taken 
        else:
            for i in range(numberdrawn):
                x = random.choice(self.contents)
                taken.append(x)
                self.contents.remove(x)
            return taken

    def exp_draw(self,numdrawn):
        exp_taken = []

        if numdrawn >= len(self.contents):
            exp_taken += self.contents
            return exp_taken 
        else:
            for i in range(numdrawn):
                x = random.choice(self.contents)
                exp_taken.append(x)    
            return exp_taken


def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
  
    success = 0
    
    
    for i in range(num_experiments):
        draws = hat.exp_draw(num_balls_drawn)
        for color in list(expected_balls.keys()):
            actual = draws.count(color)
            expected = expected_balls[color]
            if actual < expected:
                break
        else:
            success += 1
            

    print(success/num_experiments)       
    return success/num_experiments   








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)

Your browser information:

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

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

Can you share your output and test results?

How close is it?

Please also share any error message in the browser console



i get an output in the range of 0.29-0.33

In your experiement function you call the exp_draw method to draw the balls, but in that method the content of the hat remains the same after each drawing, so it’s drawing with replacement rather than drawing without replacement the challenge requires.

could you explain what drawing with replcaement and drawing without replacement means?

Suppose there’re 6 black balls, 4 red balls and 3 green balls in the hat, totally 13 balls as the test example.

Drawing without replacement: if you draw a green ball from the hat and don’t put back to the hat, there will be 2 green ball remained, totally 12 balls for the next draw. It would be less likely to draw another green ball from the hat. To draw 5 balls, there will be 8 balls remained at the end.

Drawing with replacement: You’ve drawn a green ball from the hat and put it back. So after every drawing there are 13 balls in the hat for the next draw, and the distribution of colour keeps unchanged.

It would be easy to see the probability of drawing specific combination of balls from the hat would be different for the two drawing methods.