Tell us what’s happening:
What’s happening is that I don’t understand why the program doesn’t pass the final test when I use random.shuffle() instead of random.randint() in the draw() method. Could someone please explain why it doesn’t work? Because that would be a more efficient way of doing it, I believe… the list wouldn’t have to be reassembled every time we pop() from the middle…
P.s. sorry the code is without indents, idk why but I can’t seem to make indents in these comments…
Your code so far
Here’s my Replit, just check the draw() method…
Or here:
def draw(self, amt):
if amt>len(self.contents): return self.contents
random.shuffle(self.contents)
return [self.contents.pop() for _ in range(amt)]
It draws just fine, but doesn’t pass the test with the following failure:
self.assertAlmostEqual(actual, expected, delta = 0.01, msg = ‘Expected experiment method to return a different probability.’)
AssertionError: 0.261 != 0.272 within 0.01 delta (0.01100000000000001 difference) : Expected experiment method to return a different probability.
Then I have this version of draw that passes the test:
def draw(self, amt):
if amt>len(self.contents): return self.contents
return [self.contents.pop(random.randint(0, len(self.contents)-1)) for _ in range(amt)]
I just want to know why it doesn’t work with random.shuffle(), when, in theory, I believe it should do the exact same thing…
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Mobile Safari/537.36
Challenge: Scientific Computing with Python Projects - Probability Calculator
Link to the challenge: