Tell us what’s happening:
help me with this code i am not able to complete this project… i am stuck
Your code so far
import copy
import random
class Hat:
def __init__(self, **balls):
self.contents = []
for color, count in balls.items():
self.contents.extend([color] * count)
def draw(self, num_balls):
if num_balls > len(self.contents):
return self.contents.copy()
drawn_balls = random.sample(self.contents, num_balls)
for ball in drawn_balls:
self.contents.remove(ball)
return drawn_balls
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
success_count = 0
for _ in range(num_experiments):
hat_copy = copy.deepcopy(hat)
drawn_balls = hat_copy.draw(num_balls_drawn)
balls_count = {}
for ball in drawn_balls:
if ball in balls_count:
balls_count[ball] += 1
else:
balls_count[ball] = 1
success = True
for color, count in expected_balls.items():
if balls_count.get(color, 0) < count:
success = False
break
if success:
success_count += 1
probability = success_count / num_experiments
return probability
# Example usage
hat = Hat(blue=5, red=4, green=2)
probability = experiment(hat=hat,
expected_balls={'red': 1, 'green': 2},
num_balls_drawn=4,
num_experiments=2000)
print(probability)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Probability Calculator Project - Build a Probability Calculator Project