Build a Probability Calculator Project - Build a Probability Calculator Project

Why do you keep deleting your comments?

Print everything! See what it does, see what it returns.

    def draw(self, amount):
        random_element = random.choice(self.contents)
        print("random element: ", random_element)
        print("This is what remove returns: ", self.contents.remove(random_element))
        print("Contents after remove: ", self.contents)
        return [f"{self.contents.remove(random_element)}" for ball in random_element]

Test remove to see how it works

import random

test = ["one", "two", "three"]
print(test.remove(random.choice(test)))
print(test)

Remember you need to remove amount balls from the hat, and then return them all in a list: ['yellow', 'blue, 'green']

thats the correct way of doing it according to the terminal because i got back 1 and three

but when i go to test it on my code it gives back this
code:

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 list of balls: ",self.contents)

    def draw(self, amount):
        drawn_balls = []
        for _ in range(amount):
            if len(self.contents) == 0:
                break
        random_element = random.choice(self.contents)
        print("removed balls:", self.contents.remove(random.choice(self.contents)))
        print("chosen NB: ",random_element)
        



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


cur_draw = Hat(yellow = 3, blue = 2)
print("Final drawn balls:", cur_draw.draw(2))

output:

og list of balls:  ['yellow', 'yellow', 'yellow', 'blue', 'blue']
removed balls: None
chosen NB:  yellow
Final drawn balls: None

ive changed the removed balls part i just realised i have got a random_element so why am i calling it again? dumb mistake anyway that line updated still gives me none

 print("removed balls:", self.contents.remove(random_element))

now i have been told from google that none will com up when there is no value to return, but i don’t see how there is no value here to return im telling the code to look at the original list(self.contents) and remove the random.choice i selected from it

ive even printed it out

print("drawn: ",drawn_balls.append(random_element))

yet i still get none but how are no balls drawn??
i’ve now returned the drawn_balls, wait i’ve fixed it i put self.contents above to update the self.contents then i put self.contents by itself to my list.

self.contents.remove(random_element)
        print("removed balls: ", self.contents)

This is because self.contents gets updated throughout the code, it’s like a variable almost that can get updated i know that, but why coudn’t i just put the whole thing it at once why did i need to break it up?
also im still getting none for drawn balls and therefore i am not passing test

code:

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 list of balls: ",self.contents)

    def draw(self, amount):
        drawn_balls = self.contents
        for _ in range(amount):
            if len(self.contents) == 0:
                break
        random_element = random.choice(self.contents)
        print("chosen NB: ",random_element)
        self.contents.remove(random_element)
        print("removed balls: ", self.contents)
        print("drawn: ",drawn_balls.append(self.contents))
        return drawn_balls
        
        



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


cur_draw = Hat(yellow = 3, blue = 2)
print("Final drawn balls:", cur_draw.draw(2))

output:

og list of balls:  ['yellow', 'yellow', 'yellow', 'blue', 'blue']
chosen NB:  yellow
removed balls:  ['yellow', 'yellow', 'blue', 'blue']
drawn:  None
Final drawn balls: ['yellow', 'yellow', 'blue', 'blue', [...]]

and what is that […] thing

This tells you everything you need to know.

remove() will remove a ball from the list, but it returns None.

You will need to think of a different way to get the removed ball into the final list to return.

1 Like

when i put self.contents.remove etc above that it somehow worked tho, how is that?

okay but now i’m using .pop i am getting same exact output

i = random.randrange(len(self.contents))
        print("removed balls: ",self.contents.pop(i))

why is this not passing?
output:

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):
         
        for _ in range(amount):
            if len(self.contents) == 0:
                break
        random_element = random.choice(self.contents)
        print("chosen NB: ",random_element)
        i = random.randrange(len(self.contents))
        print("removed balls: ",self.contents.pop(i))
        drawn_balls = self.contents
        print("drawn: ",drawn_balls)
        return self.contents


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


cur_draw = Hat(yellow = 3, blue = 2)
amount = 2
print("Final balls drawn",cur_draw.draw(amount))

why is my output not working?
EDIT: i’m pretty sure its because i have put in the wrong values, and that isn’t the output we want, but that’a the only output i can get

it looks like you are not saving the drawn balls anywhere, they just disappear into nothing

you need to:
draw the N required balls, and return a list with those, and at the same time remove the drawn balls from self.contents

and you have a loop that does nothing

wdym not saving them anywhere, how am i not i’ve got a line where it says drawn_ball = self.contents

[quote=“OCode, post:32, topic:714507”]

drawn_balls = self.contents
        print("drawn: ",drawn_balls)
        return self.contents

Why do you copy contents to drawn_balls and then return contents?

What happens to drawn_balls?

yeah i orginally had return drawn_balls but then it gave me the […] so i modified the code to not give me it. And when i do that the output of balls being drawn is none

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):
         
        for _ in range(amount):
            if len(self.contents) == 0:
                return self.contents
        random_element = random.choice(self.contents)
        print("chosen NB: ",random_element)
        i = random.randrange(len(self.contents))
        print("removed balls: ",self.contents.pop(i))
        drawn_balls = self.contents
        print("drawn: ",self.contents.append(drawn_balls))
        return drawn_balls


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


cur_draw = Hat(yellow = 3, blue = 2)
amount = 2
print("Final balls drawn",cur_draw.draw(amount))

could you please explain in a bit more detail i don’t understand what you mean by you have a loop that does nothing and that it looks like you are not saving the drawn balls anywhere. How?

This pop is where you remove the ball, the removed color is returned by pop. Where does it go?

EDIT: You print it but that doesn’t count.

return those balls as a list of strings

it gets returned via this tho.

print("drawn: ",self.contents.append(drawn_balls))

because self.contents gets updates and i add(drawn_balls) to self.contents which is the same instance i use to update my code