wait i think i know what you are saying, but i woudn’t know how to do this, i remove the random balls from the self.contents using the .pop(i), what i think you are saying is you should take that (i) and implement it into my code, instead of the self.contents because self.contents is the original list so it wouldn’t make sense to return that again, which is what i am doing right now with my code. How do i do this tho?
This is the exercise for you to figure out. You are understanding the problem now though.
Remember print
is not return
.
i know print is not return, but why does that matter when im returning it at the end? drawn_balls is self.contents
When you draw a ball out of the bag, it’s no longer in the bag.
It would be a very strange world if the contents of the bag was the same as the items that you’ve removed from the bag.
i’ve done it but i’m still annoyed i ask so many questions and i know it shows i’m not thinking through properly, i feel as if i’m not absorbing the information, despite doing this for 2 hours a day every day since jul the 8th, i dont have any previous coding experience , i do feel like i’m getting better every day yes, but it’s not by a lot and some days i feel like i’ve wasted time because i’ve not understood it. I know you have told me to get systems in place, but how do i improve these, because when i’m struggling with something my first thought is google this, or understand my code, or write the objectives on to a word document and tick each step you have down and are struggling with, am i meant to be comfortable with everything i’ve learned in the course before doing this project?
If you feel you are struggling that much then you might benefit from an in-person class or a tutor to supplement your study here.
In this case I think though, you were mostly trying to understand the instructions and requirements. You need to give a very careful read of each sentence, the instructions are very, very precise.
Sometimes the instructions are vague and people come here to ask for clarifications. Did you read some other threads here?
This for example, is a total misunderstanding of the instructions and I have no idea where you got it from.
What more could you ask for?
yeah i suppose you are right i do need to understand the instructions better.
i have a question in a earlier topic around 3 days ago you were responding to a FCC member doing the same project i’m doing right now, you or one of the other members put in a request that the 3rd test should get changed, because the test woudn’t pass unless you coded it in a certain order, i think that’s the issue i’m having right now, but just to double check, has that request been approved?
Yes it looks like this fix was implemented 3 days ago.
It may not be active yet but it will be live in the next update. I’m really not sure how long that takes.
In the meantime, you can workaround by implementing it like this:
If the number of balls drawn is greater than or equal to the contents of the hat, empty and return the contents directly (without changing the order)
thanks , my problem solving skills are getting better, and that comment about reading the instructions, made me focus more on them, and i’ve now done it myself. It would be great if FCC made a problem solving course too, i’m sure everybody would benefit from that.
These might have some insight
https://www.codecademy.com/resources/blog/how-to-think-like-a-programmer/
yeah these helped alot, also is it cheating if i look at others code and modify it to become my own if i understand it? like i didn’t understand how to perform a num of experiments or what it was asking me and i just looked at someone elses code and it made sense so i use theres. Because isn’t that what the help thing is for, where it says click here to see if you question has been answered already in the forum
if you are looking at someone else implementing the same exact thing for a final project, you should not do that, no
this is about the fourth step i can’t seem to understand what it is i’m suppose to code, i’m told that i should call on the function using its arguments, what i mean by this is i’m told to do this
hat = Hat(black=6, red=4, green=3)
probability = experiment(hat=hat,
expected_balls={'red':2,'green':1},
num_balls_drawn=5,
num_experiments=2000)
them i’m told i should divide the m how many times i get two red and one greenby number of experiments, i don’t understand i’ve already set the experiments to 2000 and the expected balls to two red and one green. What i’m struggling with is how do i perform n and count m
To do this, you will perform
N
experiments, count how many timesM
you get at least two red balls and one green ball
It sounds like you need a loop, right? The experiment is successful when it returns at least 2 red and 1 green balls. So you would add 1 to M, and repeat.
just to be sure, am i right in saying what the fourth step is about is getting the drawn balls from 2000 different times that it’s been drawn and then i’ll need to find how many times 2 red balls and 1 green ball has been drawn in that drawn ball and then i’d add one to count of how many times 2 red and 1 green have popped up and, thats why i’d need a loop because i will completing 2000 experiments like this.
Yes, we are trying to find the probability of a certain result. Something like a simple monte carlo method
https://en.wikipedia.org/wiki/Monte_Carlo_method
For example, what is the probability of rolling a 6-sided die and getting a 3? So we roll the die 1000 times and 3 comes up 150 times.
150 / 1000 = 0.15
So the function will return 0.15 which represents a 15% probability.
that makes sense yeah i thought that but i woudn’t need a for loop for the expected balls because it’s a dictionary, and its green = 2 so i can’t loop that, i’ve got this now but i know this doesn’t make sense but im stuck, is there a specfic function that i can put into this
`if balls == find(expected_balls):
i tried using count but that woudn’t work either
i mean i think i could use a for loop here to iterate over expected balls
like this:
for ball in expected_balls:
if count.balls == expected_balls:
What does the contents of your dictionary look like.
If you forget how to use a dictionary, look it up. Don’t make wild guesses:
count.balls
oh i’d have to use .items here for the expected balls, but still how would i check if the dictionary contains 2 red n 1 green
import copy
import random
class Hat:
def __init__(self, **kwargs):
self.contents = [k for k, v in kwargs.items() for _ in range(v)]
print("og balls: ", self.contents)
def draw(self, amount):
drawn_balls = []
og_list = self.contents.copy()
for _ in range(amount):
if amount >= len(self.contents):
self.contents.clear()
return og_list
i = random.randrange(len(self.contents))
random_element = self.contents.pop(i)
print("chosen NB: ", random_element)
drawn_balls.append(random_element)
print("removed balls: ", drawn_balls)
return drawn_balls
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
new_hat = copy.copy(hat)
print("deepcopy: ", new_hat)
for _ in range(num_experiments):
balls = copy.deepcopy(new_hat).draw(amount)
print(balls)
for ball in expected_balls.items():
if balls == expected_balls:
print()
hat = Hat(black=6, red=4, green=3)
amount = 4
print("Final balls drawn",hat.draw(amount))
probability = experiment(hat=hat,
expected_balls={
'red':2,'green':1
},
num_balls_drawn=5,
num_experiments=2000)
print(probability)
i keep getting out None which tells me something is up with my line of code, that something is wrong