Probability Calculator - 1 error

I did the project, but when testing the program, I get an error message that I can’t understand. It involves an error on line 943 of a file that I can’t access (I can’t see what’s on the line).

Could someone explain to me what is this error?

And, if possible, could you: (1) verify that the program is in accordance with the project? I ran here at the prompt and the probability is close to what was expected. (2) explain to me how to access this file?

My program and the error message are below.

import copy
import random

class Hat:
    def __init__(self, **args):
        self.reference = list()
        self.contents = list()
        self.the_hat = dict(args)
        self.extracted = list()
        self.M = int()
        self.P = float()

        for arg in args:

        a = len(self.reference)
        x = 0
        while x < a:
            y = 0
            while y < self.the_hat[self.reference[x]]:
                y += 1
            x += 1

    def draw(self, n):
        if len(self.contents) < n:
        b = 0
        while b < n:
            if len(self.extracted) >= n:
            d = random.randrange(0, len(self.contents))
            c = self.contents.pop(d)
            b += 1
        return self.extracted

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    copied_eb = copy.deepcopy(expected_balls)
    copy_num_balls_drawn = copy.deepcopy(num_balls_drawn)
    copy_num_experiments = copy.deepcopy(num_experiments)

    list_test = list()
    list_test_2 = list()
    a = 0
    while a < len(expected_balls):
        b = copied_eb.popitem()
        a += 1

    c = 0
    while c < len(list_test):
        d = 0
        while d < list_test[c][1]:
            d += 1
        c += 1

    ne = 0
    while ne < num_experiments:
        test_draw = hat.draw(copy_num_balls_drawn)

        e = 0
        list_test_3 = list()
        while e < len(list_test):
            f = test_draw.count(list_test[e][0])
            g = list_test[e][0], f
            e += 1

        h = 0
        check_list = 0
        while h < len(list_test_3):
            if list_test_3[h][1] >= list_test[h][1]:
                check_list += 1
            h += 1

        if check_list == len(list_test):
            hat.M += 1
        ne += 1

    hat.P = hat.M / copy_num_experiments
    hat.P = "{:.3f}".format(hat.P)
    return hat.P
hat1 = Hat(blue=3,red=2,green=6)
experiment(hat1, {"blue":2,"green":1}, 4, 1000)

Error message:

Probability: 0.177
ERROR: test_prob_experiment (test_module.UnitTests)
Traceback (most recent call last):
  File "/home/runner/boilerplate-probability-calculator/", line 26, in test_prob_experiment
    self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
  File "/usr/lib/python3.8/unittest/", line 943, in assertAlmostEqual
    diff = abs(first - second)
TypeError: unsupported operand type(s) for -: 'str' and 'float'

Ran 3 tests in 0.009s

FAILED (errors=1)

Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36.
Challenge: Probability Calculator
Link to the challenge:

Notice line

hat.P = "{:.3f}".format(hat.P)

This returns str type. As later hat.P is returned it is compared with the expected value, which is float and causes the error.

1 Like

Thank you so much @sanity!
I never thought of that. Thanks.
I spent a lot of time on this yesterday and today.

Now it shows a failure, something related to the calculation.
But I will change my program. I was taking a look at the @wdccollett program (like below) and mine is very confusing.
I’m taking a look at each built-in function, seeing what I didn’t know, and then reworking my program.

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