Probabilty calculator problem

Hi! i have a problem and currently no clue about what’s causing it. I don’t get why the code works well on vsCode but no on replit. this Is my code

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

class Hat:
  def __init__(self, **args):
    self.contents = []
    for k, v in args.items():
        self.contents += [k] * v

  def draw(self, n):
    draws = []
    if n > len(self.contents):
      return self.contents
            
      for x in range(n):
        random_balls = self.contents.pop(int(random.random() * len(self.contents)))
        draws.append(random_balls)
      return draws


def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
  M = 0
  color_count = 0

  for n in range(num_experiments):
    my_hat = copy.deepcopy(hat)
    balls_drawn = my_hat.draw(num_balls_drawn)
    
    for color in expected_balls:
      if balls_drawn.count(color) >= expected_balls[color]:
        color_count += 1
    if color_count == len(expected_balls):
      M += 1
    
  return M / num_experiments

and this is the error I get:

 python main.py
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    probability = prob_calculator.experiment(
  File "/home/runner/boilerplate-probability-calculator/prob_calculator.py", line 31, in experiment
    if balls_drawn.count(color) >= expected_balls[color]:
AttributeError: 'NoneType' object has no attribute 'count'
exit status 1

here is the replete link:
https://replit.com/@dnm47/boilerplate-probability-calculator#prob_calculator.py

in your code balls_drawn is None, so check your draw method. Does it always return something?

1 Like

thanks it was an indentation problem when I copied from vsCode. that problem is solved now I got another one.

 python main.py
Probability: 0.0
..F
======================================================================
FAIL: test_prob_experiment (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-probability-calculator/test_module.py", line 27, in test_prob_experiment
    self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
AssertionError: 0.0 != 0.272 within 0.01 delta (0.272 difference) : Expected experiment method to return a different probability.

----------------------------------------------------------------------
Ran 3 tests in 0.082s

FAILED (failures=1)

your M is 0 - figure out what’s wrong there

moved the color_count into the first for loop so now the M is not 0 anymore but still an error

 python main.py
Probability: 0.16533333333333333
..F
======================================================================
FAIL: test_prob_experiment (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-probability-calculator/test_module.py", line 27, in test_prob_experiment
    self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
AssertionError: 0.252 != 0.272 within 0.01 delta (0.020000000000000018 difference) : Expected experiment method to return a different probability.

----------------------------------------------------------------------
Ran 3 tests in 0.086s

FAILED (failures=1)

Now the number is nearer, I can’t check now but I would double check that you are counting correctly the expected draws

I figured out that the draw function was not working as expected so I changed it and now it pass all the tests

thank you very much for your help!

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