Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

specify the number of balls that are in each color of the hat then gives this code
hat1 = Hat(yellow=3, blue=2, green=6)
hat2 = Hat(red=5, orange=4)
hat3 = Hat(red=5, orange=4, black=1, blue=0, pink=2, striped=9)
this is a keyword argument in each of these variables, but what i don’t understand is, why is no one doing this, i feel like im missing something instead of this i see the code i currenlty have followed by nested loops.

Your code so far

import copy
import random

class Hat:
    def __init__(self,**kwargs):
        self.contents = contents
        

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    pass

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

okay, i’ve completed that step now im struggling on this part where it says return the string you remove my current updated code,

import copy
import random

class Hat:
    def __init__(self, **kwargs):
        self.contents = [k for k, v in kwargs.items() for _ in range(v)]
    def draw(self,amount):
        random.remove(contents)



def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    pass

What is your question exactly?

This is a project that you need to complete on your own. You’ll need to do your own research.

If you get stuck and have a question ask.

I don’t see a return statement?

This isn’t a thing, is it just a wild guess?

https://docs.python.org/3/library/random.html

https://www.w3schools.com/python/module_random.asp

It’s the same advice I’ve ever had for you: Test this stuff out first. Do searches. Be systematic.

Break the problem into small steps. Get the first small thing working first and build on it.

1 Like

You can open a new notebook in Google Colab and put your code in there. I like to test code quickly there.

Put your code and call your functions there.

Notebooks are good because you can run just one line of code in a block and change it and run it again, you don’t have to keep running the whole program.

You can quickly see that your init works :white_check_mark:

You can also immediately see that random.remove() doesn’t work

oh right okay that helps i’ve imported my whole code into it but i see how that can be useful for bigger projects, but isn’t it easier to copy the whole code as you don’t need to worry about selecting pieces of code? Going on to the question i want to ask you, also i’ve fixed my random.remove code based off an article from geeks for geeks, my new code is this :

import copy
import random


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

    def draw(self, amount):
        random_element = random.choice(self.contents)
        self.contents.remove(random_element)
         return ["" for ball in random_element]



def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    pass


cur_draw = Hat(yellow = 3, blue = 2)
amount = 2
print(cur_draw.draw(amount))

i’m pretty sure so far this is correct, just wanted to ask you if there is anything missing in my code that i should know about i’ve proof read it already, i just want to make sure i’m not writing wrong code.

how is this not passing i have done what it has asked no its returned as a list of strings

['', '', '', '', '', '']

this is your return, return ["" for ball in random_element], how do you expect it to return something that is not a list of empty strings?

but then what would i return?

well, what is the requirement for the draw method?

should have a argument indicating the number of balls drawn, this method should remove randomly elements from contents, returning them elements as empty strings

no, that’s wrong, read again please

ph returning them balls as empty strings, but still i’m confused on where to start

it’s not empty strings, that’s wrong. You should start understanding the requirements.

This is the paragraph

The Hat class should have a draw method that accepts an argument indicating the number of balls to draw from the hat. This method should remove balls at random from contents and return those balls as a list of strings. The balls should not go back into the hat during the draw, similar to an urn experiment without replacement. If the number of balls to draw exceeds the available quantity, return all the balls.

Imagine a real hat full of different color balls.

Your Hat class will function the same way. You put 10 balls of different colors into the hat. If you draw 3 balls, 3 balls will be removed from the hat and it will return something like this:

['yellow', 'blue, 'green']
1 Like

okay now i’ve looked for removing instances everywhere i can’t seem to figure out whats wrong with this specific line, also thanks for previous explanation the theory helped me alot,

return [f"{self.contents.remove(random_element)}" for ball in random_element]

error

Traceback (most recent call last):
  File "main.py", line 21, in <module>
  File "main.py", line 11, in draw
  File "main.py", line 11, in <listcomp>
ValueError: list.remove(x): x not in list


i know its the self part but i’ve put everything into it and yet nothing suitable comes out

also whats the process, into thinking what it needs like for example the first test i looked at others code, but there would of been people who done it themselves, without any help how do they know what to put in? What is the thought process. You have told me about systems and to have them in place and to break them down into steps, but i just feel like im lacking in the problem solving side of things

Stop guessing. Investigate like a detective, follow the clues.

Something’s not working.

What clues do you have?
What line of code does it lead you to?

and PRINT EVERYTHING! Print each operation to make sure it does what you think it’s going to do. PRINT your variables so you know what’s in them.

You are operating blind.

1 Like

i have printed it tho

print(cur_draw.draw(amount))

that prints the draw method

i don’t know how to print it out, could you please help me on that