0

I tried to code it using randint and got an attribute error for it.

import random
def generate(start,end,n):
    res=[]
    for j in range(n):
        res.append(random.randint(start,end))
    return res
start=1
end=15
n=5
print(generate(start,end,n))

The interpreter produced the following error:

AttributeError: module 'random' has no attribute 'randint'

This code doesn't raise an error in idle.

  • This code works. What is the name of the `.py` file you are running this in? Did you name it *random.py*? – Joshua Schlichting Aug 27 '19 at 11:46
  • I did not name it random.py –  Aug 27 '19 at 11:47
  • 1
    First of all, try importing `random` in a Python shell. If this does not work, then you can be sure that random library is not imported properly – shuberman Aug 27 '19 at 11:47
  • 1
    Possible duplicate of [Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"](https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) – shuberman Aug 27 '19 at 11:48

1 Answers1

0

Check your files name! In your case “random” is key word, you can not use "random" as a file name. Take care that no files are named random.py.

Parham
  • 116
  • 10