Scientific Computing with Python Projects - test_prob_experiment fail

Hey everyone.
I am stuck on this last test. This is the result I get:

image

Probability is always 0.294. I just can’t find why.
This is the experiment method:

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
  probability = 0
  ballsDrawnDict = {}
  ballsDrawnList = []
  times = 0
  included = False

  for item in range(num_experiments):
    
    ballsDrawnList = hat.draw(num_balls_drawn)
     
    for item in ballsDrawnList:
      if not item in ballsDrawnDict:
        ballsDrawnDict[item] = 1
      else:
        ballsDrawnDict[item] += 1

    for item in expected_balls:
      for item2 in ballsDrawnDict:
        #Comparing each element of the expected balls with each element of a dictionary containing the drawn balls
        if item == item2:
          #If balls match, then the amount is compared
          if expected_balls[item] > ballsDrawnDict[item2]:
            #If the amount of expected balls is higher than those drawn, the loop brakes
            included = False
            break
          else:
            #If not, the "included" variable is set to true
            included = True
      if included == False:
        break

    #If after all the checks the expected balls are included, we sum 1 to the times the condition was fulfilled
    if included:
      times += 1

    included = False
    ballsDrawnList.clear()
    ballsDrawnDict.clear()

  probability = times / num_experiments

  return probability

Link to my Replit: boilerplate-probability-calculator - Replit

I’d appreciate any hint on why I am getting this results.
Thanks!

Comparing expected_balls with the ballsDrawnDict it seems that case when expected ball is not drawn at all is incorrectly counted.
For example this case is included in the successful experiments:

expected_balls={'blue': 2, 'green': 1}, ballsDrawnDict={'red': 2, 'blue': 2}
1 Like

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