Tell us what’s happening:
There is an implementation of
Hat.draw that involves picking balls one by one by getting a random index, copying it, and popping it from
Hat.contents, and I assume that is the intended one, because it passes all the tests without issue.
However, I initially tried a slightly simpler method, as follows:
def draw(self, number): random.shuffle(self.contents) n = min(number, len(self.contents)) result = self.contents[:n] self.contents = self.contents[n:] return result
This one will fail
test_hat_draw in the test module, giving the following error:
AssertionError: Lists differ: ['red', 'red'] != ['blue', 'red'] First differing element 0: 'red' 'blue' - ['red', 'red'] ? ^ - + ['blue', 'red'] ? ^^^ : Expected hat draw to return two random items from hat contents.
Although I did not change the random seed, my different implementation causes the result to be different from the expected “random” result that would be given by a different implementation.
Is there something necessarily wrong with the implementation I provided, or is the test module just not robust enough to handle the difference?
Your code so far
My code on replit is available here: fcc-probability-calculator - Replit
Your browser information:
User Agent is:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.96.36.199 Safari/537.36
Challenge: Scientific Computing with Python Projects - Probability Calculator
Link to the challenge: