Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

Hi, really unsure why the final test case isnt passing. The probability values im getting are roughly around the same range (0.374,0.3768,0.354 etc etc). What exactly is expected especially since it says every test result is different too?

Your code so far


def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    success=0
    for i in range(num_experiments):
        drawn=hat.draw(num_balls_drawn)
        correct=True
        for j in expected_balls:
            if expected_balls[j]>drawn.count(j):
                correct=False
        if correct:
            success+=1
        hat.contents.extend(drawn)
    return success/num_experiments

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

Please show all of your code

import copy
import random

class Hat:
    def __init__(self,**kwargs):
        self.contents=[]
        for i in kwargs:
            self.contents.extend([i]*kwargs[i])
    def draw(self,num):
        if num>len(self.contents):
            removed=self.contents.copy()
            self.contents=[]
        else:
            removed=[]
            for i in range(num):
                removing=random.choice(self.contents)
                removed.append(removing)
                self.contents.remove(removing)
        return removed

 
        

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    success=0
    for i in range(num_experiments):
        drawn=hat.draw(num_balls_drawn)
        correct=True
        for j in expected_balls:
            if expected_balls[j]>drawn.count(j):
                correct=False
        if correct:
            success+=1
        hat.contents.extend(drawn)
    return success/num_experiments

Yes, every test result should return a different probability and your code does that. I do not know what error you are getting but I think it is similar to the issue I had.

It appears it does not like unused variables. Like you, I used a couple of unused variables in some for-loops and I was getting an assertion error. Try replacing your unused variables “i” in your draw and experiment methods with a “_”.

Try replacing the unused variables of your for loops with a blank. That solved my problem.

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