Hi guys, would appreciate some help and feedback on my code. I have an issue passing test_prob_experiment in this exercise.
This was the error given: 0.256 != 0.272 within 0.01 delta (0.016000000000000014 difference) : Expected experiment method to return a different probability.
0.256 was the closest and best i could go. Feel free to look into my code written below and would appreciate any feedback or changes that i could make to it.
import random
class Hat:
def init(self, **color):
self.contents = list()
self.outofhat = list()
for k, v in color.items():
while v > 0:
self.contents.append(str(k))
v -= 1
def draw(self, number):
ballsdrawn = []
if number > len(self.contents):
self.contents.extend(self.outofhat)
self.outofhat.clear()
for i in range(number):
random_draw = random.choice(self.contents)
self.contents.remove(random_draw)
ballsdrawn.append(random_draw)
self.outofhat.append(random_draw)
return ballsdrawn
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
M = 0
conditions_to_meet = len(expected_balls.items())
for i in range(num_experiments):
conditions_met = 0
balls_drawn = hat.draw(num_balls_drawn) #balls_drawn is a list of balls drawn
print(balls_drawn)
for k,v in expected_balls.items():
if balls_drawn.count(k) >= v:
conditions_met += 1
if conditions_met == conditions_to_meet:
M += 1
probability = M/num_experiments
return probability