Hello everyone and a very Happy New Year!
So I’ve been trying to code the probability calculator and keep getting an error in the third test.
I’m pretty sure it’s with my draw() method.
But I’ve literally gone through all of the posts about it on this forum and tried tinkering with my code but to no avail.
Would appreciate any help.
Here’s my Repl: https://replit.com/@ShirshoDasgupta/boilerplate-probability-calculator
Here’s the code in an embed:
import copy
import random
# Consider using the modules imported above.
class Hat:
##
###
def __init__(self, **col_balls):
### creates list to be written into
self.contents = []
### loop runs through each key (named after a color) in col_balls dictionary
for color in col_balls:
### loop runs from 0 to the number of balls for each color
for number in range(col_balls[color]):
### appends list with color
self.contents.append(color)
##
## Defines method to draw balls
### defines method; accepts number of balls to be drawn as an argument
def draw(self, no):
### creates list to be written into
drawn = []
### checks if number of balls to be drawn is more than the number of given balls
if no > len(self.contents):
### stores entire list of given balls as drawn
drawn = copy.deepcopy(self.contents)
### clears initial list of given balls
self.contents.clear
### returns entire list of given balls
return drawn
else:
for i in range(no):
### selects the index of ball to be picked
picked = random.choice(range(0, len(self.contents)))
### adds ball to the list of drawn balls
drawn.append(self.contents[picked])
### removes ball from initial list
self.contents.pop(picked)
### return the drawn balls
return drawn
##
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
### creates counter for number of successful experiments
count = 0
### loop runs through the designated number of times the experiment is to be conducted
for i in range(0, num_experiments):
### creates copies to work on of initial list so it is the same after each experiment
hat_copy = copy.deepcopy(hat)
### creates copy of list of expected balls to be drawn so it is the same after each experiment
expected_copy = copy.deepcopy(expected_balls)
### computes and stores list of drawn balls
colors_drawn = hat_copy.draw(num_balls_drawn)
### loop runs through each ball (designated by color-name) in list of drawn balls
for color in colors_drawn:
### checks if the ball-color is in the list of expected balls
if color in expected_copy == True:
### the number of balls of that color in the list of expected balls is deducted by one
expected_copy[color] = expected_copy[color] - 1
else:
pass
### checks if all the values corresponding to each key in the list of expected balls is zero
if(all(x <= 0 for x in expected_copy.values())):
### adds one to the counter for successful experiments
count = count + 1
return count/num_experiments
And here’s the error:
Probability: 0.0
..F
======================================================================
FAIL: test_prob_experiment (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/shirshodasgupta-probability-calculator/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.0 != 0.272 within 0.01 delta (0.272 difference) : Expected experiment method to return a different probability.
----------------------------------------------------------------------
Ran 3 tests in 0.181s
FAILED (failures=1)