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
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.
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:
for i in source:
color = i
n_hat = int(source[color])
while n_hat > 0:
n_hat -= 1
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.