I think i dont get the test
import random
import copy
class Hat:
def __init__(self, **balls):
self.contents = []
for k, v in balls.items():
for i in range(v):
self.contents.append(k)
def draw(self,number):
listofdraws = []
choices = []
if number > len(self.contents):
return self.contents
else:
for i in range(number):
choice = random.choice(self.contents)
listofdraws.append(choice)
#Removing the choice to append it later
self.contents.remove(choice)
choices.append(choice)
for i in choices:
self.contents.append(i)
return listofdraws
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
times = 0
expected = []
#Get the expecteds in list(str)
for k,v in expected_balls.items():
for i in range(v):
expected.append(k)
for time in range(num_experiments):
choices = hat.draw(num_balls_drawn)
#Each time the loop begins it does a copy
c_expected = copy.copy(expected)
c_choices = copy.copy(choices)
count = 0
for i in c_expected:
# it loops in every object in
# ['blue', 'blue', 'green']
if i in c_choices:
#If for example 'blue' is in (copy)_choices, gets removed
c_choices.remove(i)
count+=1
if count == len(expected):
times += 1
else:
break
return times/num_experiments
The code fails in the second and third tests, but it seems to work well
(at least in my head).
Traceback (most recent call last):
File "C:\xxx\test_module.py", line 27, in test_prob_experiment
self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
AssertionError: 0.248 != 0.272 within 0.01 delta (0.02400000000000002 difference) : Expected experiment method to return a different probability.
What am i doing wrong?
I think I did not understand something of the challenge, like a missconception