def draw(self, number):
if int(number) > len(self.contents):
return self.contents
_drawn = []
for _ in range(number):
selected = random.choice(self.contents)
_drawn.append(selected)
self.contents.remove(selected)
return _drawn
Please Tell us what’s happening in your own words.
Learning to describe problems is hard, but it is an important part of learning how to code.
Also, the more you say, the more we can help!
In the first case your function returns self.contents list, but you also need to clear it out
by clearing out do you mean to do smthng like self.contents=,
if so i have already done it
i used another draw method which is like
class Hat:
def __init__(self, **kwargs):
self.contents = [k for k, v in kwargs.items() for _ in range(v)]
def draw(self, n):
n = min(n, len(self.contents))
return [self.contents.pop(random.randrange(len(self.contents))) for _ in range(n)]
i have been stuck in this darw part for over a 3 days now, nd i cannot understand what clearing out the contents mean
By clearing out I mean make the contents list empty
Your present method is nice, but here is one non-obvious issue:
In the case n > len(self.contents) the method should return a list of all the elements of self.contents in exactly the same order but self.contents should be empty.
so if i give self.contents = [ ], and then return self.contents will it suffice
so if i give self.contents = [ ], and then return self.contents will it suffice
so i am building probability calculator and i get an error titled " The draw
method should behave correctly when the number of balls to extract is bigger than the number of balls in the hat. " is there any reason as to why?
Yes, you can just reassign self.contents to the empty list.
also i do not know if it related to beta , but in this course"sscietific computation with python" we give the code directly rather than replit like it used to in the past
Now you firstly reassign self.contents to the empty list and then return self.contents which is the empty list already
You should consider this way:
I need to create a list which contains all the values in the same order as self.contents but it should be different object.
And also I need to clear self.contents.
Finally I should return the newly created list.
ill try it that way thankyou
thankyou it worked, i am grateful for the help
Please post actual code instead of screenshots