Probability calculator - I cant find my mistake

Dear all, I struggle on solving the provided task. The hat experiment returns “Probability: 0.22666666666666666” as the result, not the expected one.
Can someone help me out?
Here is my code for this part:

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
  raw_str = str(hat).split(",")
  data = raw_str[:-2]
  experiment_data = []
  exp_balls = []
  for key in expected_balls:
    for x in range(expected_balls[key]):
  for x in range(num_experiments):
    temp_data = data.copy()
    temp_balls_drawn = copy.copy(num_balls_drawn)
    for y in range(temp_balls_drawn):
      rdn = random.randint(0, len(temp_data)-1)
      del temp_data[rdn]
  counter = 0
  for x in experiment_data:
    fits = copy.copy(exp_balls)
    for y in x:
      if y in fits:
      if fits == []:
        counter += 1
  return (counter / num_experiments)

I would strongly appreciate a hint or a correction.
Thank you !!!

I get this error trying to run this function in my project locally:

  File "/home/gray/src/work/fcc-sc-probability-calculator/", line 86, in experiment
    rdn = random.randint(0, len(temp_data)-1)
  File "/usr/lib/python3.9/", line 339, in randint
    return self.randrange(a, b+1)
  File "/usr/lib/python3.9/", line 317, in randrange
    raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0, 0, 0)

which is probably happening when there is only one element in the array from which you are choosing. If you are not seeing it, then it’s probably due to version differences.

I notice that you are not using the draw() method you should have defined in class Hat(), which looks to be making things more difficult as that method should be doing the work of selecting balls at random without re-implementing it in the experiment function. You could make a copy of the hat and perform the experiment on a new version of the hat every time. For me, creating a function like this is always easier if I break out the comparison logic into a separate function, something like is_good_draw().

If you are committed to this way, then the best way to begin to find the problem is to print your drawn ball list, your desired ball list, and whether it was successful or not for every experiment and search for the times that it should have been a successful experiment but was not counted as one. Since your probability is low, there should be times where the program thinks it is a bad draw and it really was a good one.

It would be helpful if you would provide a link to your entire code (not just one function). That would also allow us to read the entire error message because I don’t know what exactly the problem with the error is. Is it to low? To high? Slightly off? Extremly off?

Also if you struggle, start by adding comments to your code and maybe some more meaningful names for variables. At least I have trouble following what the entire thing is doing with something like copy.copy(...)

It would be easier if you just copied the link to the :wink:
Because we still need the full error message ^^°

To be honest, I have trouble following your code and the difference suggests you are making some minor mistake.
What bothers me right now is that the challenge made you write a draw() function but you don’t use it. You also create quite a lot of variables and that’s really hard to follow.

All I could really recommend is just printing out values at different points of the funciton to figure what it actually is doing and what it is supposed to do.
Although I tested that and it didn’t help me.

Or, you know, try to think of an easier method to do the task.
I mean, in the end you have to create two lists and check if all elements of one are present in the other. This task should not result in code with over half a dozen variables which you can’t read yourself.

And quick headsup: If I understand it correctly, you create all 1000 experiments first and then check every single one. That’s a huge waste of memory.

Duuude, I got it…
While I do highly recommend you figure out a shorter way to write the function and actually use your hat-class:
data = raw_str[:-2] needs to be data = raw_str[:-1]
Second index of list-slicing is exclusive, meaning you cut out an actual element from your hat.
With that you will get an error for trying to draw 4 balls out of a hat with 3 elements - so you will need to solve that.

It’s possible, as I did it.


Dude how could I made this mistake … stupid !!!
Coding a simple “if / else” in addition will make this code pass the test.