Tell us what’s happening:
Hi! I’m having trouble with the Probability Calculator assignment. The problem arises in the following line of code:
for color in expected_balls.contents: (Line 52) in the link I shared
The error says: ‘dict’ object has no attribute 'contents’
Nevertheless, when I run the code in my computer (not on the Replit starter code), I don’t get such error. I don’t know what’s going on, since I’ve already checked that expected_balls.contents is a List, not a dictionary.
Maybe the problem arises in the first lines of code, where I define contents as a class variable (an empty list -line 6-) and in line 9 as an instance variable self.contents = . I did this to assure this list is cleaned every time an experiment is initialized. But maybe there’s another way, I don’t know.
Can anyone help me? thanks!
Your code so far
import copy
import random
class Hat: #*args define una cantidad de argumentos variable
contents= []
def __init__ (self, **kwargs):
self.contents = [] #The list contents is "cleaned" when a new instance of
#the class is initialized
#Conversion of the dictionary kwargs into a list with format:
# Example: (yellow=3, blue=2) ----> ['yellow','yellow','yellow,'blue','blue']
for key,value in kwargs.items():
for j in range (value):
self.contents.append(key)
def draw (self, n_balls):
#The method withdraws balls from self.contents
balls_taken= []
if n_balls > len(self.contents)-1: #balls extracted greater than available
balls_taken = self.contents
self.contents = []
return self.contents, balls_taken
for i in range(n_balls):
rand_extraction = random.randint(0, len(self.contents)-1)
extracted = self.contents.pop(rand_extraction)
balls_taken.append(extracted)
return balls_taken #is a list
#---------------------------------------------------------------------------------------------------------
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
experiment_count = 0
for i in range(num_experiments):
hat_copy = copy.deepcopy(hat) #Copy of the hat object inside the function to avoid
#modifying the original object (DEEP COPY)
expected_balls_dict= dict()
#Dictionary expected_balls_dict is filled
for color in expected_balls.contents:
expected_balls_dict[color] = expected_balls_dict.get(color,0) + 1 #OK, es un diccionario, como count_balls
count_balls = dict()
#Balls are extracted and the dictionary count_balls is filled:
extracted_balls = hat_copy.draw(num_balls_drawn) # ['blue', 'yellow', 'yellow',...] List format OK
for color in extracted_balls:
count_balls[color] = count_balls.get(color,0) + 1 # {'yellow': 3, 'green': 3, 'blue': 2} Dictionary format OK
#Comparision between dictionaries count_balls and extracted_balls_dict
aux = 0
for color in expected_balls_dict:
if color not in count_balls:
break #Experiment ends
if count_balls[color] >= expected_balls_dict[color]:
aux += 1
if aux == len(expected_balls_dict):
experiment_count += 1
probability= experiment_count/num_experiments
return probability
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36
Challenge: Probability Calculator
Link to the challenge:
boilerplate-probability-calculator-1 - Replit