Scientific Computing with Python Projects - Probability Calculator

Tell us what’s happening:
Describe your issue in detail here.

Hi, I think I’ve done the code, but an error appears.
“ValueError: empty range for randrange()”

Can someone help me?

my code - boilerplate-probability-calculator - Python Repl - Replit

Your code so far

import copy
import random


# Consider using the modules imported above.

class Hat:
    def __init__(self, **balls):
        self.balls = balls
        self.contents = []
        for ball, num in balls.items():
            for i in range(num):
                self.contents.append(ball)

    def draw(self, number):
        pop = []
        for i in range(number):
            pop.append(self.contents.pop(random.randrange(len(self.contents))))

        return pop


def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    happen = 0
    for i in range(num_experiments):
        cop_hat = copy.deepcopy(hat)
        cop_exp_ball = copy.deepcopy(expected_balls)
        cor_pop = cop_hat.draw(num_balls_drawn)

        top = 0
        for ball, num in cop_exp_ball.items():
            if cor_pop.count(ball) >= num:
                top += 1
        if top == len(expected_balls):
            happen += 1

          
    print(happen/num_experiments)

    pro = happen / num_experiments

    return pro

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

I found the problem!

1 Like

Hey, may I ask what the solution was? I ran into something similar and am not sure whether I really solved it or just silenced the possibility that there could be an error :smiley: Best regards

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