Build a Probability Calculator Project - Cannot get tests 3 and 4

Tell us what’s happening:

Cannot get tests 3 and 4 to pass /draw method: I am deleting the drawn items from contents in any case, while outputting the drawn list. Still tells me draw method should behave correctly for drawn balls > balls in hat/ experiment function: returns the probability that seems right during my experiments (1 if drawing all balls), still browser console states ‘0.0 != 1.0 with delta 0.1’ and test 4 expects a different probability. I tried multiple solutions (some working in forum) with no luck

Your code so far

import copy
import random

class Hat:
    
    def __init__(self, **kwargs):
        keyList = list(kwargs.keys())
        valueList = list(kwargs.values())
        self.contents = []
        for amount in valueList:
            index = valueList.index(amount)
            for i in range(amount):
                self.contents.append(keyList[index])

    def draw(self, ballsDrawn):
        if ballsDrawn >= len(self.contents):
            drawResult = self.contents.copy()
            self.contents.clear()
        else:
            drawResult = random.sample(self.contents, ballsDrawn)
            for i in drawResult:
                self.contents.remove(i)
        return drawResult



def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    M = 0
    expectedList = []

    for k,v in expected_balls.items():
        k = [k] * v
        expectedList.extend(k)

    for _ in range(num_experiments):
        hatCopy = copy.deepcopy(hat)
        drawList = hatCopy.draw(num_balls_drawn)
        if compare_lists(drawList, expectedList):
            print('good ---- e:', expectedList, 'd:', drawList)
            M += 1
        else:
            print('bad ---- e:', expectedList, 'd:', drawList)
    
    return M/num_experiments

def compare_lists(list1, list2):
    for item in set(list2):
        if list1.count(item) < list2.count(item):
            return False
    return True




hat1 = Hat(yellow=3, blue=2, green=6)
hat2 = Hat(red=5, orange=4)
hat3 = Hat(red=5, orange=4, black=1, blue=0, pink=2, striped=9)

hat4 = Hat(black=6, red=4, green=3)

print(experiment(hat4, {'red':2, 'green':1}, 5, 2000))



Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

There’s a subtle bug in the Hat class. Take a look at the following example:

hat1 = Hat(yellow=3, blue=2, red=2)
print(hat1.contents)

My goodness, I really do not think I would have caught that ever. I changed the list declaration to use the extend() method as in experiment and it now works perfect. I am still not sure how the instance you provided would not work while the ones from the instructions seemed to work fine.
Thank you!

    def __init__(self, **kwargs):
        keyList = list(kwargs.keys())
        valueList = list(kwargs.values())
        self.contents = []
        for amount in valueList:
            index = valueList.index(amount)
            for i in range(amount):
                self.contents.append(keyList[index])

The way it happened was, when there’s couple entries in the list (valueList) with the same value. The index = valueList.index(amount) would point to the same index in both cases, effectively skipping the name of the second ball with keyList[index].

Since kwargs is a dictionary, getting the both relevant values, with lesser chance for mixup, could be done with items method:

for key, value in dictionary.items():
    print(key, value)