Reputation: 437
I am using Sublime Text3. I am encountering a problem with the choices
attribute with the random
module. I do not have the same name in any path or directory. The other attributes of random work just fine.
import random
import string
letters = string.ascii_lowercase
print(letters)
gen = random.choices(letters, k=16)
print(gen)
Here is the error code:
abcdefghijklmnopqrstuvwxyz
Traceback (most recent call last):
File "/home/anon/.config/sublime-text-3/Packages/User/test.py", line 6, in <module>
gen = random.choices(letters)
AttributeError: 'module' object has no attribute 'choices'
What are the common causes of this problem?
Upvotes: 3
Views: 2284
Reputation: 382
I encountered the same error when I used Python 2.7.
As a workaround, I simply used random.choice
instead of random.choices
, in a way as
foo = [random.choice(my_list) for _ in range(50)]
Although we do not have random.choices
in Python 2.7, we have random.choice
(the former is multiple choices with duplicates, while the latter single choice).
Upvotes: 0
Reputation: 14216
It would seem you are using a version of Python that is older than 3.6 which is when random.choices
was introduced. You can see it listed at the bottom of this function description here
You can verify your version by running
import sys
sys.version
Upvotes: 4
Reputation: 828
There's no random.choices
in Python 2. You can use random.sample
in Python 2.
gen = random.sample(letters, k=16)
random.choices
is included in Python 3
Upvotes: 4