# Probability Calculator - wrong results

Tell us what’s happening:
Getting two test module errors out of 3:

1. Expected to reduce number of items from contents
2. Different probability expected

Also, how do I use the copy module already included in the file?
Your code so far

import copy
import random
import collections

class Hat:
def init(self, **kwargs):
self.no_of_balls = kwargs
self.drawn_balls = list()
# print(self.no_of_balls)
self.contents = list()
for each_item in self.no_of_balls:
x = self.no_of_balls[each_item]
for each_colour in range(x):
self.contents.append(each_item)

``````def draw(self, draw_balls):
used_numbers = list()
self.drawn_balls = []
for each_time in range(draw_balls):
if draw_balls > len(self.contents):
self.drawn_balls = self.contents
break
y = random.randint(0, len(self.contents)-1)
temp = y
if y not in used_numbers:
self.drawn_balls.append(self.contents[y])
used_numbers.append(y)
else:
while temp == y:
y = random.randint(0, len(self.contents) - 1)
self.drawn_balls.append(self.contents[y])
used_numbers.append(y)

return self.drawn_balls
``````

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
expected_balls_list = list()
total_events = num_experiments
favourable_events = 0

``````for each_item in expected_balls:
x = expected_balls[each_item]
for each_colour in range(x):
expected_balls_list.append(each_item)

for each_event in range(num_experiments):
for each_subevent in range(num_balls_drawn):
balls_drawn = hat.draw(each_subevent)
if collections.Counter(expected_balls_list) == collections.Counter(balls_drawn):
favourable_events += 1
else:
continue
probability = favourable_events/total_events
return probability
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36`.

Challenge: Probability Calculator

Link to the challenge:

Hi rishikj29,

The assignment asks to copy the hat, so you might want to look into the copy library to see what the available options are (it’s only 2, so no sweat ).
Then with each draw you are suposed to take the balls out of the (copied) hat, which will reduce the number of items from contents (actually remove these from the list before the next ball is taken out). If you don’t do this, the calculation will be wrong, since the chances of taking the desired color are influenced (and the probablility is incorrect).

Is this helping you in the right direction?

Thanks! Claimed my certification!

1 Like

Nice work, congrats!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.