Probability calculator: probability value too low

my code ran pretty well but only managed to pass 2/3 tests.

I am not able to identify why my probability output is much lower than the expected from the tests modules.

can anyone take a look at my code and give me some feedback? any help will be very much appreciated! thank you!

here’s the link to my code:

1 Like

So if the probability is low, either the draw or the check is wrong. Since the draw passes other tests, check the probability with this modification:

        if check:
            print(f"pass: drawn: {hat.dict} expected: {expected_balls}")
            correct += 1
            print(f"fail: drawn: {hat.dict} expected: {expected_balls}")

and if you check the output you will find lines like

fail: drawn: {'red': 1, 'blue': 3} expected: {'blue': 2, 'red': 1}

which should be passes but are not counted that way. That means your check

        check = all(item in hat.dict.items() for item in expected_balls.items())

is not working correctly. It looks like it only checks if all the items (keys for dicts) are in both dicts, and not that the numbers in the draw are greater than or equal to the ones in the expected.

One of your self.contents is also typo’d as self.content too, but I don’t think that matters here.


You are absolutely right! My check was not functioning how i thought it would be. Thank you very much!

I replaced the check with these 2 for loops to match the key value pairs in 2 dictionaries. It is a bit long winded and would like to ask if there is a shorter way of doing this? maybe like in a dictionary comprehension etc.
Thank you.

type or paste code here
```    for i in range(num_experiments):
        for k1, value1 in hat.dict.items():
            for k2, value2 in expected_balls.items():
                if k1 == k2 and value1 >= value2:
                    correct += 1
        if correct >= len(expected_balls):
            correct = 0
            match += 1
            correct = 0

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