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