# Failing to passthe Probability Calculator due to error "The draw method should behave correctly when the number of balls to extract is bigger than the number

``````def draw(self, number):
if int(number) > len(self.contents):
return self.contents
_drawn = []
for _ in range(number):
selected = random.choice(self.contents)
_drawn.append(selected)
self.contents.remove(selected)
return _drawn
``````

The hat should be emptied when a number greater than the number of balls in the hat is used. You are returning the whole content of the hat, but the hat is still full.

If this doesnâ€™t solve your problem, please share the entire code.

I tried both to empty the contents and returning an original copy of the array, it is not working â€¦ this is the instruction â€śIf the number of balls to draw exceeds the available quantity, return all the balls.â€ť â€¦ If â€śxâ€ť total balls exists , only â€śyâ€ť balls need to be drawn - y > x then return x ?

``````class Hat:
def __init__(self, **obj ):
self.obj = obj
self.contents = ("".join([ f"{key}," * value for key,value in obj.items() if value != 0 ])).split(",")[:-1]
self.originalData = copy.deepcopy(self.contents)

def draw(self, number):
if number > len(self.contents):
return self.contents
_drawn = []
for _ in range(number):
select = random.choice(self.contents)
_drawn.append(select)
self.contents.remove(select)
return _drawn
``````

how have you tried to empty the contents and returning an original copy of the array?

Good Day ilenia, am using the below code:

``````class Hat:
def __init__(self, **obj ):
self.obj = obj
self.contents = ("".join([ f"{key}," * value for key,value in obj.items() if value != 0 ])).split(",")[:-1]
self.originalData = copy.deepcopy(self.contents)

def draw(self, number):
if number > len(self.contents):
return self.contents
_drawn = []
for _ in range(number):
select = random.choice(self.contents)
_drawn.append(select)
self.contents.remove(select)
return _drawn

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
match = 0
for _ in range(num_experiments):
i = 0
objCopy= copy.deepcopy(hat) # List will run out of data if no copy is created
results = objCopy.draw(num_balls_drawn)
for key,value in expected_balls.items():
count = results.count(key)
if count >= value:
i+=1
if i == len(expected_balls):
match+=1
return match / num_experiments
``````

Iâ€™ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the â€śpreformatted textâ€ť tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

there is no way this is emptyint the hat, remember that a return stops the function

Hi there, I changed your code as below and it worked perfectly:

â€” removed solutionâ€“

1 Like

Gracious , I have tried multiple times but my error was returning the original copy of the array when condition failed I think. Thank you

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original posterâ€™s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

yo , did u copy the original array everytime u called the method? im stuck in this too

Yo, please open a new topic

In the future, please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.

Thank you.

1 Like