# Scientific Computing with Python Projects - Probability Calculator

Tell us what’s happening:
Hello guys! I know that my code is wrong, however i can’t identify in my code what is wrong… could you help me to identify pls?

Your code so far

``````import random as rd
import copy

class Hat():
# **kwards permite colocar n argumentos
def __init__(self, **kwargs):
#é criado um dicionário
self.balls = kwargs

#vamos acessar os "keys" dos dicionários
# todos os "keys" estarão na lista a
def dicionario(self):
a = (self.balls.keys())
#print(a)
return a

#somar número de bolas na simulação
def soma(self):
s=0
for i in self.balls:
s += self.balls[i]
#print('total de bolas: ',s)
return s

def contents(self):
a1 = list(self.balls.keys())
a2 = list(self.balls.values())
d = []
for i in range(len(a2)):
a3 = a2[i]
while a3>0:
d.append(a1[i])
a3 = a3-1
#print('lista d:',d,type(d))
return d

def draw (self,n):
n = int(n)
Qtd_balls = len(self.balls)
if (Qtd_balls<n):
n = Qtd_balls
return n
else:

a1 = list(self.contents())
x=[]
for i in range(n):
a2 = rd.randrange(0,n)
x1 = a1.pop(a2)
x.append(x1)
#return a1
return x

#return x,a1,len(a1)
def experiment(hat, expected_balls,num_balls_drawn,num_experiments):

qtd = 0
for i in range(num_experiments):
#copiar o hat
hatcopy = copy.deepcopy(hat)
#vamos agora fazer a retirada das balls
sorteio = hatcopy.draw(num_balls_drawn)
h1 = expected_balls.items()
if sorteio == h1:
qtd +=1
else:
qtd +=0
return qtd/num_experiments
``````

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

What errors are you getting?

Hello Sanity, thanks for answer!

below the errors:

## FAIL: test_hat_class_contents (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-probability-calculator/test_module.py”, line 12, in test_hat_class_contents
self.assertEqual(actual, expected, ‘Expected creation of hat object to add correct contents.’)
AssertionError: <bound method Hat.contents of <prob_calculator.Hat object at 0x7f416a954a60>> != [‘red’, ‘red’, ‘red’, ‘blue’, ‘blue’] : Expected creation of hat object to add correct contents.

## ====================================================================== FAIL: test_hat_draw (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-probability-calculator/test_module.py”, line 18, in test_hat_draw
self.assertEqual(actual, expected, ‘Expected hat draw to return two random items from hat contents.’)
AssertionError: Lists differ: [‘red’, ‘blue’] != [‘blue’, ‘red’]

First differing element 0:
‘red’
‘blue’

• [‘red’, ‘blue’]
• [‘blue’, ‘red’] : Expected hat draw to return two random items from hat contents.

## ====================================================================== FAIL: test_prob_experiment (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-probability-calculator/test_module.py”, line 28, in test_prob_experiment
self.assertAlmostEqual(actual, expected, delta = 0.01, msg = ‘Expected experiment method to return a different probability.’)
AssertionError: 0.0 != 0.272 within 0.01 delta (0.272 difference) : Expected experiment method to return a different probability.

The first error suggests that `contents` instance variable of the `Hat` class is not what’s expected. It should be a list, but it appears to be a method instead.

Looking at the second error, the `draw` method is returning different balls than expected. Could you explain your logic from it?

I’d try to go over the errors one by one, as the changes to make one of the tests pass might affect the other tests.

Hey Sanity

First Error:
In this method, i do a list “d” which is the output of this fuction

`````` def contents(self):
a1 = list(self.balls.keys())
a2 = list(self.balls.values())
d = []
for i in range(len(a2)):
a3 = a2[i]
while a3>0:
d.append(a1[i])
a3 = a3-1
#print('lista d:',d,type(d))
return d
``````

In draw i use the list done by method contents.
i use “randrange” to output a range position from list “a1” and pop to delete this element from the list

``````  def draw (self,n):
n = int(n)
Qtd_balls = self.soma()
if (Qtd_balls<n):
n = Qtd_balls
return n
else:
a1 = list(self.contents())
x=[]
for i in range(n):
a2 = rd.randrange(0,Qtd_balls-1)
x1 = a1.pop(a2)
x.append(x1)
Qtd_balls -=1
return x
``````

If you have any more question please don’t hesitate to contact me

Rgds

That’s still not a list, but method returning list.

What arguments does `random.randrange` take?