Probability calculator result problem

Hi, I started the Freecode Camp Scientific Computing with python. I went trough the videos and began completing the projects. I’m trying to finish the last one but I get an error from the last unit test, I get 0.257 instead of the 0.272 expected. I have been looking for an answer but didin’t find any solution.

here’s my code
and the traceback :

Sorry for the commented lines these are print I use to check on what’s going on.

Thanks to everyone making FreeCodeCamp possible !
Cheers

An error like this means you are not counting some draws that are good, so the bug has to be where you are counting good experiments in your experiment() function. If you take your code:

    for color in balls_drawn:
      if (color in expected_balls) and (color not in colors):
        if (balls_drawn.count(color) >= expected_balls[color]):
          colors.append(color)
          #print (color, expected_balls[color], balls_drawn.count(color))
          #print (color, expected_balls[color])
          #print (colors)
      if (colors == expected_colors) or (colors == expected_colors.reverse()):
        print("GOOD")
        M += 1
        break

and add some print() statements:

    good = False
    for color in balls_drawn:
      if (color in expected_balls) and (color not in colors):
        if (balls_drawn.count(color) >= expected_balls[color]):
          colors.append(color)
          #print (color, expected_balls[color], balls_drawn.count(color))
          #print (color, expected_balls[color])
          #print (colors)
      if (colors == expected_colors) or (colors == expected_colors.reverse()):
        # print("GOOD")
        good = True
        print(f"GOOD drawn: {balls_drawn} expected: {expected_balls}")
        M += 1
        break

    if not good:
        print(f"BAD drawn: {balls_drawn} expected: {expected_balls}")

You’ll get ouput like this:

BAD drawn: ['red', 'blue', 'blue', 'green'] expected: {'blue': 2, 'green': 1}

which shows that your code missed a good draw. I would check how you are comparing the drawn balls and expected balls, especially the last conditional. Since there are more than two colors, I don’t think colors and expected_colors can be expected to be equal or to the other’s reverse.

Good luck.

Hi jeremy.a.gray thanks for your answer. Your were right my code was missing some good draws becaus I wasn’t comparing the right lists.

I was able to complete this project thanks to your advice.
Thanks a lot

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