Scientific Computing with Python Projects - Probability Calculator. Failed test_hat_draw and test_prob_experiment

Tell us what’s happening:
Hey there, I’m as well trying to get the probability calculator working, but it resists quite a long time yet :smiley:
Two problems:
1.) I can’t get the test_hat_draw test to conclude. I always draw ‘red’,‘red’, but there seems something to be off with whatever seed-stuff, but I cant get into how to do it differently.
2.) My probability seems to be a bit more off. Maybe I have watched the code to long but I cant find any problems in the counting.

Could you please help in any ways?

Your code so far
You can find the code in boilerplate-probability-calculator - Replit
Sorry for the many print statements. Didn’t want to introduce (nicer) logging here…
Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

Hi, have a look at your init statement. I learnt that it should be right at the top after the class, like the previous two projects.

I used statement like these to test if I am getting the correct content.
hat3 = Hat(red=3,blue=2)
print(hat3.contents)
print(hat3.draw(2))
print(hat3.contents)

I think your hat is getting too many contents from the beginning.

If the hat and draw tests fail, then the remaining test should fail as well since they depend on the implementation of the hat so any probability results at this point can be disregarded.

There are some fundamental problems with the code that may be causing problems. First, you are using class variables:

class Hat:
    contents = list()
    unmodified_contents = list()
    expected = list()
    exp_dict = dict()
    n_balls = 0

Class variables should be used only when information must be shared between objects and be the same between objects. You should be using instance variables for all these (if necessary) and they should be set in __init__().

Second, your code is not really using Hat objects itself:

    def l2l(self,target,source):
        for i in source:
             color = i
             n_hat = int(source[color])
             while n_hat > 0:
                 target.append(color)
                 n_hat -= 1
        return target

I assume you are trying to convert the dictionary or balls for the hat initialization to a list here but you are not using self. You’re using target in this code that should be changing self and the caller is passing in self.contents for target anyway.

My advice is to stop worrying about the experiment for now and work on the initialization of the hat and the draw method. Both can be much simpler than they currently are. Work incrementally; for instance, just print the balls in the initialization. Then work on the loop to convert the dict to your list (stored in an instance variable). That should pass the hat test. Then use the same process on the draw method. Don’t worry about the experiment until the hat and draw tests pass.

When you finally get to the experiment, use the copy module there. If your probabilities are wrong, log the expected balls, the drawn balls, and how your code classified each trial and look for misclassified trials.

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