If you want what I think you want, then what you want is the random
module.
If you're using a list's length to determine your choices, you'll want to make sure that the choices are in range of the list. Fortunately using the range()
feature does this automatically, so by getting the len()
of the list before indexing, you'll never fail.
import random
my_choices = range(0, len(some_list))
print random.choice(my_choices)
EDIT: Ah, perhaps you're seeing the selection is out of range? Setting sticksOnBoard
to the length of your list will cause an IndexError
.
>>> mylist = [x for x in range(0, 16)] #this list will have items 0 through 15, for a total of 16
>>> mylist[16] #this is 1 too large for the list, however
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
If you always want the last item in the list, you can use mylist.pop()
to always remove the last item in the list. If you're looping over a list, this could cause problems.
You can also index by using mylist[-1]
, which will always index the very last item in the list.