Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

Stuck on the part “The draw method should behave correctly when the number of balls to extract is bigger than the number of balls in the hat.”

I dont get it, how do I tackle this? My code is given below:

Your code so far

import copy
import random 

class hat:

    def __init__(self,red = None,orange = None,black = None,blue = None,pink = None,striped = None,yellow = None,green = None):

        self.red = red
        self.orange = orange 
        self.black = black
        self.blue = blue 
        self.pink = pink
        self.striped = striped
        self.yellow = yellow 
        self.green = green

        lst = {'red':red,
               'orange':orange,
               'black':black,
               'blue' : blue,
               'pink' : pink,
               'striped' : striped,
               'yellow' : yellow,
               'green' : green
        }

        self.contents = []
        for key,value in lst.items():
            if value is not None :
                self.contents += value*[str(key)]

        self.copy = []
        self.copy = self.contents.copy()

        

    def draw(self,number):

        self.hat_list = []
        self.contents = self.copy.copy()
        
        if number >= len(self.contents) :
                
                self.contents.clear()
                self.hat_list = (self.copy.copy())
    

        else:
                
                for i in range(number):
                     
                     temp = self.contents.index(random.choice(self.contents)) 
                     self.hat_list.append(self.contents.pop(temp))
                     
            
        return self.hat_list
                     
                     
        

Your browser information:

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

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

I know that we can use the **kwargs thing in the start to make the code even more efficient. Im just doing it slowly. But im stuck on the third test :frowning:

print self.hat_list when the number of balls drawn is greater than the balls it contains. What does it contain?

This is what i get

Hi

Try returning the “self.hat_list” inside the if statement

doest seem to work :frowning: using this. Is my logic applied correct?

the test that is failing has a color that is not included here, you want to use kwargs like you were saying.

2 Likes

ooooooooooo i seeeeeee thank u! :slight_smile:

THANK YOU SO MUCH. YOU WERE RIGHT AND IT MAKES SENSE!!!

cuz like if the color is any other color not specified then it’ll show wrong on the test

I got it correct now :slight_smile:
I was stuck on this for a day lol

I got the first three correct now. Thanks again

2 Likes

Both your if and else statement should return a list , and inside your “experiment” function make sure you create a copy of the “Hat” object that will be used for drawing random balls

Already resolved, thanks

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