Scientific Computing with Python - Probability Calculator Tests

Hello ppl.
I am on the final project of Scientific Computing with Python, and I am stuck with the tests.
Something strange (at least for me) happens. If I run the tests, the test_hat_draw and test_prob_experiment. But what I come to ask is about the first one.

I always get the same list with 2 red balls, resulting in a failed test. But if I comment everything and from main I just call the draw method, I do get different results.

And to make it even stranger, if I comment the first and third tests, test_hat_draw passes correctly:

How is it possible?
Thanks everyone in advance, and sorry for my poor english.

Ps: Initially I used more images, but as a new user, I can only use 1 :person_shrugging:

Calling the draw method from main, with everything else commented:

The message I get when running all the tests normally:
image

Please provide your full code (the actual code, not a screenshot).

Is the link to my replit enough? Here it goes:

Thanks!

You’re throwing data into a global space

    global ballsList
    global contents
    global secondaryContents

Thats nearly always a bad idea.

At the beggining, declaring those “globals” inside functions was the only way I found to not get errors related with the variables not being declared. Somehow I missunderstood its use.
Anyway, I have commented every global declaration, but the results are just the same. It still puzzles me how the fact that a test is preceded by another, changes its results.

That’s because they are still global. You are creating those variables once and reusing the same variable every time. You want to look into the difference between class and instance variables in Python.

I’ll follow your advice, thanks!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.