Probability calculator project

import copy
import random

class Hat:
    def __init__(self, **kwargs):
        self.kwargs = kwargs
        self.contents = []
        for k, v in self.kwargs.items():
            for i in range(v):

    def draw(self, draw_num):
        self.random_drawn_balls = []
        if draw_num > len(self.contents):
            draw_num = len(self.contents)
        for i in range(draw_num):
            self.random_drawn_balls.append(self.contents.pop(random.randint(0, len(self.contents) - 1)))
        return self.random_drawn_balls

If I use the above code to execute the following:

hat1 = Hat(blue=4, red=10, green=6)
for i in range(20):
    drawn_balls = hat1.draw(5)

The output is 4 lists with items expected, but the rest are all empty which isn’t intended, how should I deal with that?

This is where you need to start thinking about the copy module. In your code, you are drawing from (and changing ) the same hat. This is clear from your numbers: you draw 4 balls 5 times (4 * 5 = 20) , and the rest of your draws will be zero because the hat is now empty.

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