[Tutor] Having trouble with a component of the random module
Alan Gilfoy
agilfoy at frontiernet.net
Thu Aug 28 17:29:17 CEST 2008
Quoting W W <srilyk at gmail.com>:
>> The number of items I want from a list is smaller than the population
>> (number of items) in the list, so it should work.
>>
>> In this specific case, I'm asking for one item from a five-item list.
>
> Are you sure?
>
> change this:
> for card in random.sample(SpecialA, SA):
>
> to this:
>
> print "Special: ", SpecialA, "\nSA: ", SA
> for card in random.sample(SpecialA, SA):
>
> then give us the output. I'll bet that your SA value is never reset to 1.
>
There is a part of my front-end code that declares the SA variable as
equal to 1 for the card set in question, and feeds that into the
processing function.
Changing the block of code, as you suggest, still gets me a traceback:
Special: []
SA: 1
Traceback (most recent call last):
File "C:\Documents and Settings\Alan\My
Documents\_Alan_Programming\trunk\BoosterPackMaker\BoosterPackMaker.py", line
104, in <module>
SixList_PackChooser(C_list, UC_list, R_list, BL_list,
SpecialA_list, SpecialB_list, C, UC, R, BL, SA, SB)
File "C:\Documents and Settings\Alan\My
Documents\_Alan_Programming\trunk\BoosterPackMaker\PackGenerator.py",
line 367, in SixList_PackChooser
for card in random.sample(SpecialA, SA):
File "C:\Python25\lib\random.py", line 303, in sample
raise ValueError, "sample larger than population"
ValueError: sample larger than population
So, I'm asking my program to pick 1 item from a 0-item list. How there
are zero items in the list, I don't know. That must be the problem.
>
> You've obviously researched enough to know this:
>>>> mylist = [1, 2, 3, 4, 5]
>>>> from random import sample
>>>> sample(mylist, 3)
> [2, 1, 4]
>>>> sample(mylist, 10)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/lib/python2.5/random.py", line 303, in sample
> raise ValueError, "sample larger than population"
> ValueError: sample larger than population
>
Okay, asking for 3 items from a 5-item list works, and asking for 10
items from a 5-item list doesn't work. It should be trying to pick 1
item from the 5-item SpecialA list, but somehow the SpecialA list has
zero items in it.
>
> So give the print statement before your for loop a try and see what you come
> up with.
Okay, I cut'n'pasted it before the traceback message.
>
> HTH,
> Wayne
Thank you.
-----------------------------------------------
SixList_PackChooser(C_list, UC_list, R_list, BL_list, SpecialA_list,
SpecialB_list, C, UC, R, BL, SA, SB)
^ The error occurs in this function.
The program picks C number of items from C_list and displays them just fine.
The program picks UC number of items from UC_list and displays them just fine.
The program picks R number of items from R_list and displays them just fine.
The program picks BL number of items from BL_list and displays them just fine.
Looking at my user-interface/front-end, it looks like the function's
being fed proper values.
More information about the Tutor
mailing list