Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

can someone help me where did my logic go wrong?
I assume my code handles all the edge cases but still fails to pass the test cases.

Your code so far

import copy
import random

class Hat:
    def __init__(self, **args):
        self.contents = [] 
        for key, val in args.items():
            for _ in range(val):
                self.contents.append(key)
    def draw(self, n_balls):
        draw_hat = []
        if n_balls > len(self.contents):
            return self.contents
        else:
            while n_balls > 0:
                if n_balls > len(self.contents):
                    draw_hat.extend(self.contents)
                r_index = random.randint(0, len(self.contents)-1)
                ball_drawn = self.contents.pop(r_index)
                draw_hat.append(ball_drawn)
                n_balls-=1  
                print(self.contents)
        print('\n===============================')
        return draw_hat

h1 = Hat(black =6, red =4, green=3)
print(h1.draw(16))

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    M = 0
    hat1 = copy.deepcopy(hat)
    for exp in range(num_experiments):
        hat_list = hat.draw(num_balls_drawn)
        draw_result = currHat(hat_list)
        s=compare_draw(expected_balls, draw_result)
        if s:
            M+=1
        hat= copy.deepcopy(hat1)
    return M/num_experiments
def currHat(hat_list ):
    hat = {}
    for ball in hat_list:
        if ball not in hat:
            hat[ball]=1
        else:
            hat[ball]+=1
    return hat

def compare_draw(expected_balls, draw_result):
    true_result = []
    for key in expected_balls.keys():
        if key in draw_result:
            true_result.append(expected_balls[key] == draw_result[key])
    return all(true_result)
#print(experiment(hat=h1,expected_balls={'red':2,'green':1,num_balls_drawn=2,num_experiments=3))


    

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

Hi @karthiksanugula511,

For Step 3, although it doesn’t outright say it, what you should be returning are all of the remaining balls and the contents of the hat object should be empty because remember that you are drawing balls from a hat so those balls should not be there anymore.

Happy Coding!

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