Probability Calculator - Python | FreeCodeCamp

preview_player
Показать описание
I go through how to do the probability calculator on FreeCodeCamp.
I learned about the python random and copy packages. I learned quite bit while making this, and I hope you learn a lot as well. Thanks for watching and have a great day!

Рекомендации по теме
Комментарии
Автор

This guy out there single handedly saving my life lol

azuree
Автор

Hey my man, I just wanted to say thanks for posting these! You're really good at breaking everything down as yo go along without a bunch of fluff. I appreciate it greatly.

brokenmold
Автор

The draw function below will work well and give the expected results
def draw(self, num_balls):
if num_balls > len(self.contents):
return self.contents
balls = []
for i in range(num_balls):
random_ball = random.randint(0, (len(self.contents)-1))


return balls

geraldkpu
Автор

Thank you man, helped me a lot, keep up the good work.

mvelisompukuzela
Автор

The deepcopy saved my life, thank you!

eddg
Автор

I'm just a beginner in the programming area. Thank you for clearing things out!

carlosrscoelho
Автор

random.sample in the draw method returns the expected results:

def draw(self, numBalls):
removed_balls = list()
if (numBalls > len(self.contents)):
return self.contents

removed_balls = random.sample(self.contents, numBalls)
for ball in removed_balls:
self.contents.remove(ball)

return removed_balls

jadefreel
Автор

Tried using randrange. Worked previously but when i tried correcting for Errors it seems to say number out of range and doesn't stop at the draw_number given....keeps going except makes the fixed contents list same as the shrinking contents understand why really (as i say i tried in pycharm with my own variables and it didn't do this afaik)

jorriffhdhtrsegg
Автор

I dont get the random seed concept in python here, in my case the seed was like 'pre-determined' so my test for 'draw 2 random balls' always end up with 2 blues (which will always fail because the test requires 1 red and 1 blue), because the randomized array i made will always make a certain order.

ucuphis
Автор

Bro, thanks a lot for all of the videos you make for the python course, you really help me a lot, and i finally complete the course, THANKS YOU SO MUCH BRO! GREAT JOB!

josuearistimuno
Автор

Thanks a lot bro. Helped me massively
God bless

jolualuko
Автор

I did this:

def draw(self, balls):
draw_list = list()
if balls > len(self.contents):
return self.contents
for i in range(balls):
idx =

self.contents.pop(idx)
print(draw_list)
return draw_list

and all tests passed.

zinz
Автор

I think the problem of 0.252 instead of 0.272 is because of random.random func. I try the and got the right one.

簡郁軒-rp
Автор

Thanks for posting these videos. They are helping me get through these assignments.
My question is with this prob calculator. I’m getting the error in main:
AttributeError: module ‘prob_calculator’ has no attribute ‘experiment’
I’ve been trying to resolve this but can’t seem to find out why it does this. Did you come across this if so how where you able to fix it?

yippiecalle
Автор

Hi I know this is old. Am currently doing this and it's different testing on the site than a few years ago. . It passes all tests according to the test module.


But the website now checks in the draw function if the num is greater than length of contents. That line fails the check. According to the console it should return an empty self. contents . (Or so i think its asking that) I tried
return self.contents.clear() . Breaks the experiment function. I'm not entirely sure what the instructions are asking, but the console expects an empty self . Contents

me-fvxb
Автор

thanks alot for your great effort
the problem that occured in the end of the video in the difference in probability can be avoided using random.choice() instead of random.random() in draw() method

abdelrahmanmahany
welcome to shbcf.ru