I am attempting the Probability Calculator from freecodecamp, which can be found Here
In a class function to randomly select n items from a list, I access the contents of my list via self.contents, which is an instance variable for whatever hat filled with balls I make. My function looks as such:
def draw(self,num_balls_drawn):
randomlist = []
templist = self.contents
if num_balls_drawn <= len(templist):
for x in range(num_balls_drawn):
popped = templist.pop(random.randint(0, len(templist)-1)) # random has been imported
randomlist.append(popped)
return randomlist
else:
return self.contents #If you ask for more items than there are in list, just returns list
Therefore, when I create a class instance using this code:
class Hat:
def __init__(self,**kwargs):
self.__dict__.update(kwargs)
contents=[]
for key in self.__dict__:
for x in range(self.__dict__.get(key)):
contents.append(key)
self.contents= contents
And then create an instance with hat = Hat(blue=4, red=2, green=6), then testing my functions withprint(hat.contents) print(hat.draw(7)) print(hat.contents) print(hat.draw(7))
I would hope to be given a list of ['blue', 'blue', 'blue', 'blue', 'red', 'red', 'green', 'green', 'green', 'green', 'green', 'green'] for hat.contents and a list such as ['blue', 'blue', 'red', 'green', 'blue', 'blue', 'green'] for hat.draw(7)
However, on the second attempt of using these statements, I instead am returned ['blue', 'blue', 'green', 'green', 'green'] and
['blue', 'blue', 'green', 'green', 'green']
Both of which are somehow a length of only 5.
It seems despite setting up a temporary list templist, my self.contents is still shortened everytime I pop an item out of templist.
If anyone could offer a solution to my Issue that would be appreciated.