Tell us what’s happening:
I am always failing test three. My output is 0.99 which wrong. But I’m not sure what it is i’m doing wrong. I double checked the creation of the hat class and draw methods and those are consistent with how you’d do that experiment manually. I feel like I’m either misunderstanding what constitutes an experiment, I’m assuming the hat resets after each loop in the number of experiments. Or am I misunderstanding what counts as a successful match? If for example, the requirements for a success is drawing 2 blue, 1 red, would drawing 2 blue and 2 red count since the latter is a subset of the former?
Your code so far
import random
class Hat:
def __init__(self, **kwargs):
self.contents = []
for key in kwargs:
for i in range(kwargs[key]):
self.contents.append(key)
def draw(self, number):
balls_drawn = []
if number > len(self.contents):
return self.contents
for i in range(number):
ball = random.choice(self.contents)
balls_drawn.append(ball)
self.contents.remove(ball)
return balls_drawn
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
successes = 0
ball_dict = {}
valid_draw = False
for i in range(num_experiments):
test_subject = copy.deepcopy(hat)
balls_drawn = test_subject.draw(num_balls_drawn)
num_match = 0
for ball in balls_drawn:
ball_dict[ball] = ball_dict.get(ball, 0) + 1
for key in expected_balls:
if expected_balls[key] <= ball_dict.get(key, 0):
num_match += 1
if balls_drawn == hat.contents:
valid_draw = True
if len(expected_balls.items()) == num_match:
valid_draw = True
if valid_draw:
successes += 1
return successes / num_experiments
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0
Challenge: Scientific Computing with Python Projects - Probability Calculator
Link to the challenge: