Tell us what’s happening:
I have run my code in sublime and each time I run it I get a different probability (between 0.38 and 0.41).
I am using the ‘hat = Hat(black=6, red=4, green=3)’ case in the description to run my tests
I can pass tests 1-3 but can’t pass test 4
I have tried clicking the ‘run the tests’ button a few times in case there was a chance occurrence of the same probability
Your code so far
import copy
import random
class Hat:
def __init__(self, **kwargs):
if not kwargs:
print("no values passed")
else:
self.contents = [
key
for key, value in kwargs.items()
for value in range(value)
]
def draw(self, draw_count):
ball_list = []
num_to_draw = 0
if draw_count > len(self.contents):
num_to_draw = len(self.contents)
else:
num_to_draw = draw_count
while num_to_draw > 0:
random_ball = round(random.random() * (len(self.contents) - 1))
ball_list.append(self.contents[random_ball])
self.contents.pop(random_ball)
num_to_draw -= 1
return ball_list
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
good_draws = 0
experiments = num_experiments
expected_list = [
key
for key, value in expected_balls.items()
for value in range(value)
]
while num_experiments > 0:
num_experiments -= 1
hat_copy = copy.deepcopy(hat)
draw_list = hat_copy.draw(num_balls_drawn)
# see if all the expected_list are in draw_list
list_count = len(expected_list)
for i in expected_list:
if i in draw_list:
draw_list.remove(i)
list_count -= 1
if list_count == 0:
good_draws += 1
return good_draws / experiments
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Challenge Information:
Build a Probability Calculator Project - Build a Probability Calculator Project