Scientific Computing with Python Projects - Probability Calculator (SO CLOSE)

I’m at my wit’s end here.
I cant manage to get the last test achieved, due to the probability result given is double what its supposed to be! (0.548 != 0.272)
Can anyone point out what exactly is the line that is causing the problem?
I feel like I’m missing a fundamental part of understanding probabilities

This is what I’ve got so far

import copy
import random
# Consider using the modules imported above.

class Hat:

  ##A hat will always be created with at least one ball. The arguments passed into the hat object upon creation should be converted to a contents instance variable. contents should be a list of strings containing one item for each ball in the hat. Each item in the list should be a color name representing a single ball of that color. For example, if your hat is {"red": 2, "blue": 1}, contents should be ["red", "red", "blue"].
  def __init__(self, **kwargs):
    self.list_balls = []
    self.contents = []
    for color,count in kwargs.items():
    #for loop passes KWargs from hat defining call into list of tuples
    for color,count in kwargs.items():
      for x in range(count):
    self.list_for_prob = copy.copy(self.contents)
  def __str__(self):
    return f'{self.list_for_prob}, {self.contents}'

  def draw(self, num):
    if num > len(self.contents):
      return self.contents
    removed = []
    result = random.sample(self.contents,num)
    for x in result:
    return removed
def experiment(hat, expected_balls, num_balls_drawn, num_experiments) ->None:
  counter = 0
  for x in range(num_experiments):
    hat_copy = copy.deepcopy(hat)
    result = hat_copy.draw(num_balls_drawn)
    ball_match = True
    res_dict = {}
    for ball in result:
      if ball not in res_dict:
        res_dict[ball] = 1
        res_dict[ball] += 1
    ball_match = True
    for balls in expected_balls.keys():
      if result.count(balls) < expected_balls[balls]:
        ball_match = False
      if ball_match:
        counter += 1

  prob = counter/num_experiments
  return prob

if I divide prob in 2, i pass the test on line 27 but not the next one on line 32

someone Pls Help!

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

Could you explain what ball_match variable is supposed to be doing?

It was supposed to be checking if the dictionary for the results of the draw, has at more than or equal to the values in the dictionary for the expected balls,
in the end I decided to transmute the dictionary of expected balls to a list of strings and remove them through a for loop then checking if the list is empty.
This gave me the correct result

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