I have some code that looks something like this:
import random n = 0 while n <= 50: n = n+1 a = random.randint(1, 16) b = random.randint(1, 5) print n, ". ", a, "-", b, "= "
For some reason, when running it, I get the following error: AttributeError: 'module' object has no attribute 'randint'
. However, I have no problems when running the same random.randint queries in IDLE. How can I fix this?
The Python "AttributeError module 'random' has no attribute 'randint'" occurs when we have a local file named random.py and try to import from the random module. To solve the error, make sure to rename any local files named random.py . Here is an example of how the error occurs in a file called random.py .
Python Random randint() Method The randint() method returns an integer number selected element from the specified range. Note: This method is an alias for randrange(start, stop+1) .
We call randint a pseudo-‐random number generator (PRNG) since it generates numbers that appear random but are not truly random.
randint(0, 10) will return a random number from [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 10]. Use a random. randrange() function to get a random integer number from the given exclusive range by specifying the increment. For example, random.
You have another module called "random" somewhere. Did you name your script "random.py"?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With