I can’t pass the test:
The draw method should behave correctly when the number of balls to extract is bigger than the number of balls in the hat.
why?i have tried a lot and i can’t pass it.
Your code so far
import copy
import random
# Consider using the modules imported above.
class Hat:
def __init__(self, **kwargs):
self.contents = []
for key, value in kwargs.items():
for _ in range(value):
self.contents.append(key)
def draw(self, balls: int):
if balls >= len(self.contents):
return self.contents
drawn = random.sample(self.contents, k=balls)
for ball in drawn:
self.contents.remove(ball)
return drawn
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
expected_no_of_balls = []
for key in expected_balls:
expected_no_of_balls.append(expected_balls[key])
successes = 0
for _ in range(num_experiments):
new_hat = copy.deepcopy(hat)
balls = new_hat.draw(num_balls_drawn)
no_of_balls = []
for key in expected_balls:
no_of_balls.append(balls.count(key))
if no_of_balls >= expected_no_of_balls:
successes += 1
return successes/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
well if the number of balls to be drawn is larger the number of balls in the hat,
then we change the number of balls to be drawn so that it equals the number of balls in the hat,
and then we simply draw randomly and remove a ball from the hat each time,
isn’t this what i’m supposed to to?