Probability Calculator - 1 error

Hello!!
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:
            self.reference.append(arg)

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

    def draw(self, n):
        if len(self.contents) < n:
            self.contents.extend(self.extracted)
            self.extracted.clear()
        b = 0
        while b < n:
            if len(self.extracted) >= n:
                self.contents.extend(self.extracted)
                self.extracted.clear()
            d = random.randrange(0, len(self.contents))
            c = self.contents.pop(d)
            self.extracted.append(c)
            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()
        list_test.append(b)
        a += 1

    c = 0
    while c < len(list_test):
        d = 0
        while d < list_test[c][1]:
            list_test_2.append(list_test[c][0])
            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
            list_test_3.append(g)
            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:

0.177
Probability: 0.177
..0.259
E
======================================================================
ERROR: test_prob_experiment (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-probability-calculator/test_module.py", 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/case.py", 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.