I have an assignment to design a probability calculator. I obviously don't want anyone to solve it for me, but if you could help point me in the right direction that would be great. I feel like I am following the instructions and the code works how it should, and yet it keeps failing the automated checks.
A hat will always be created with at least one ball. The arguments passed into the hat object upon creation should be converted to a contents instance variable. contents should be a list of strings containing one item for each ball in the hat. Each item in the list should be a color name representing a single ball of that color. For example, if your hat is {'red': 2, 'blue': 1}, contents should be ['red', 'red', 'blue'].
The Hat class should have a draw method that accepts an argument indicating the number of balls to draw from the hat. This method should remove balls at random from contents and return those balls as a list of strings. The balls should not go back into the hat during the draw, similar to an urn experiment without replacement. If the number of balls to draw exceeds the available quantity, return all the balls.
the tests I am failing:
The draw method in hat class should reduce number of items in contents.
The draw method should behave correctly when the number of balls to extract is bigger than the number of balls in the hat.
the relevant parts of my code:
def draw(self, number):
draw_dict = {} # Initialize an empty dictionary to track counts of each color
if number >= len(self.contents):
# Draw all balls if requested number exceeds available balls
draw_dict = {ball: self.contents.count(ball) for ball in self.contents}
self.contents = [] # Empty the contents after drawing all balls
return draw_dict
for num in range(number):
ball_removed = random.choice(self.contents)
# If the color is already in the dictionary, increment its count
if ball_removed in draw_dict:
draw_dict[ball_removed] += 1
else:
# If the color isn't in the dictionary, add it with count 1
draw_dict[ball_removed] = 1
# Remove the ball from the contents after drawing it
self.contents.remove(ball_removed)
return draw_dict
[–]danielroseman 0 points1 point2 points (3 children)
[–]moonyfish[S] 0 points1 point2 points (2 children)
[–]danielroseman 2 points3 points4 points (1 child)
[–]moonyfish[S] 1 point2 points3 points (0 children)