Im trying to make a guessing game in python where the computer generates a random number and the user has to guess it. To generate a random number i used the randint function from the random module yet i get this error message. ImportError: cannot import name ‘index’ from ‘operator’
I tried to generate a random number using randint function from the random module , yet this error message keeps popping up : from operator import index as _index
ImportError: cannot import name ‘index’ from ‘operator’
2
This usually happens the you have a file in your search path with the same name of a file in the standard library ..In this case, you likely have an operator.py
file lying around which shadows the stdlib’s own operator
module.
That said, there is no need to use operator.index
in learning purpose-code – it is meant mostly for completion of a “non douple underscore syntax” to reach all possible operations in Python, and would only be useful in an app built with somewhat refined architecture, and eager to preserve semantics.
You probably just want int
instead of operator.index
.
Either way, rename your operator.py
file to some other name.