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
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
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.
should have a argument indicating the number of balls drawn, this method should remove randomly elements from contents, returning them elements as empty strings
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.
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:
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
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