'How to correct TypeError with choices() missing 1 required positional argument: 'population'

I want list of entries to display when calling the random page function but I keep getting this error choices() missing 1 required positional argument: 'population'.



Solution 1:[1]

I had this problem due a typo recently.

Aparently you are using the recommended 'secrets' PNG and not 'random' pseudoPNG. I had the following code:

#!/usr/bin/python3
"""
Will get 10 random letters...
from the lowercase abcdefghijklmnopqrstuvwxyz
"""
import string
import secrets
response = secrets.SystemRandom.choices(string.ascii_lowercase, k=10)
print(response)

But secrets.SystemRandom is a class, so I just changed it to secrets.SystemRandom() and the issue was fixed.

Fixed code:

#!/usr/bin/python3
"""
Will get 10 random letters...
from the lowercase abcdefghijklmnopqrstuvwxyz
"""
import string
import secrets
response = secrets.SystemRandom().choices(string.ascii_lowercase, k=10)
print(response)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ricardo Vargas