Build a Probability Calculator Project - Build a Probability Calculator Project - final test timing out

Tell us what’s happening:

My experiment keeps timing out. It times out in seconds. I’m not sure what to do about that.

Your code so far

import copy
import random

class Hat:

    
    contents = []

    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            for _ in range(value):
                self.contents.append(key)
        
        

    def __str__(self):
        pass
    
    def draw(self, number_of_balls):
        used = []
        result = []
        too_big = []
        if len(self.contents) < number_of_balls:
            too_big = self.contents.copy()
            self.contents = []        
            return too_big

        for _ in range(number_of_balls):
            random_ball = random.randint(0, len(self.contents)-1)
            while True:
                if random_ball in used:
                    
                    random_ball = random.randint(0, len(self.contents)-1)
                else:
                    used.append(random_ball) 
                    result.append(self.contents[random_ball])
                    self.contents.pop(random_ball) 
                    break
        
        return result

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    #initializing the list of expected balls and the total number of passing experiments
    expected = []
    total = 0
    # creating the expected balls list
    for key, value in expected_balls.items():
                for _ in range(value):
                    expected.append(key)
    # running the experiment the required number of times
    for _ in range(0,num_experiments):
        single_success = 0
        total_success = False
        # copyint the volitile hat.contents to another variable to put back later
        drawn_contents = hat.contents.copy()
        drawn = hat.draw(num_balls_drawn)
        
        for i in range(len(expected)):
            if expected[i] in drawn:
                popper = drawn.index(expected[i])
                drawn.pop(popper)
                single_success += 1 
                   
        if single_success == len(expected):
            total_success = True
        
        if total_success:
            total += 1
        # returning the original contents of hat.contents
        hat.contents = drawn_contents.copy()
    
    return total/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

When I use the example in the project(the one with 2000 as the num_experiments), it runs without an issue. I get a different probability returned when it runs multiple times. I’m not sure what is timing out.

the console states:
// running tests The

experiment

method should return a different probability. (Test timed out) // tests completed

there is nothing listed in the F12 console for the final test

Could you explain how the draw method of Hat works?

OK. I rewrote the draw, and now I am getting another error that tells me nothing about what is actually wrong. F12 only tells that .59 != 1.

AssertionError: 0.59 != 1.0 within 0.01 delta (0.41000000000000003 difference) : Expected experiment method to return a different probability.

def draw(self, number_of_balls):
used =
result =
too_big =

    # checking if the number of balls is more than what the hat contains
    if len(self.contents) < number_of_balls:
        # had to add this because, even though it was working, the test was failing because contents wasn't empty
        too_big = self.contents.copy()
        self.contents = []        
        return too_big

    # now that we know the number of balls is no more than what is in the hat, start looping
    for _ in range(number_of_balls):
        # get a random item index
        random_ball = random.randint(0, len(self.contents)-1)
        result.append(self.contents[random_ball])
        self.contents.pop(random_ball) 
                
    
    return result

Hmm, this is strange. I created two hats:

hat = Hat(black=6, red=4, green=3)
hat2 = Hat(yellow=5, blue=9)

Then tried to draw all balls from the second one:

print(hat2.draw(14))

And result contained also balls from first hat…

[‘blue’, ‘yellow’, ‘black’, ‘yellow’, ‘blue’, ‘blue’, ‘green’, ‘blue’, ‘blue’, ‘red’, ‘blue’, ‘red’, ‘black’, ‘black’]

1 Like

Awesome, thanks so much.

1 Like

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