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):

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
        y = random.randint(0, len(self.contents)-1)
        temp = y
        if y not in used_numbers:
            while temp == y:
                y = random.randint(0, len(self.contents) - 1)

    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):

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
probability = favourable_events/total_events
return probability

Your browser information:

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 :wink:).
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.