Problem with the Probability Calculator in the Sientific Computing with Python Course

Hi guys,

I can’t pass the last test and i think the problem is, that the balls don’t go back in the contents of the hat. So with the next experiment there are fewer balls in the contents.
I tried to change the code of the Hat class but then i did not pass the second test.

I don’t know how i can reset the hat after every experiment.

My code so far
Replit

import copy
import random
# Consider using the modules imported above.

class Hat:
    def __init__(self, **balls):
        self.contents = []
        for ball, amount in balls.items():
            self.contents += [ball] * amount
    
    def draw(self, count):
        if count >= len(self.contents): return self.contents
        return [self.contents.pop(random.randint(0, len(self.contents)-1)) for _ in range(count)]
        

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    count = 0
    for _ in range(num_experiments):
        balls = h.draw(num_balls_drawn)
        result = {ball: balls.count(ball) for ball in set(balls)}
        if not all( b in result for b in expected_balls ): continue
        if all( result[b] >= c for b, c in expected_balls.items() ):
            count += 1

    return count / num_experiments

My browser information:

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

Challenge: Probability Calculator

Link to the challenge:

You should clone the hat and draw from a fresh clone each iteration/experiment.

2 Likes

Add a “.reset()” method to he hat?
I mean, the intended method is to use the already importet modules - but if you already think about resetting, you could also think about implementing that :wink:

1 Like

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