-1

I am following a Python textbook and it contains an error in a sample program below:

# Random Access
# Demonstrates string indexing

import random

word = "index"
print "The word is:", word, "\n"

high = len(word)
low = -len(word)
for i in range(10):
    position = random.randrange(low, high)
    print "word[", position, "]\t", word[position]

raw_input("\n\nPress enter to exit.")

Python complains with the following error:

File "C:\Python27\random.py", line 3, in <module>
import random   

File "C:\Python27\random.py", line 11, in <module>
    position = random.randrange(low, high) AttributeError: 'module' object has 
    no attribute 'randrange'

Any ideas how to fix this? I am new to programming and python. I have used the "import random" code before and it worked fine.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
BBedit
  • 7,037
  • 7
  • 37
  • 50
  • 2
    Don't name your file "random.py". Also, don't use the Python dir as your workspace – JBernardo Feb 01 '14 at 17:55
  • @JBernardo thank you, it works now. The textbook did not warn of this! – BBedit Feb 01 '14 at 18:01
  • Please don't add the word 'SOLVED' to your question title; mark the solution as accepted instead. For a self-answer that takes a little longer, but your question may be useful to others in the future too, solved or not, a duplicate or not. – Martijn Pieters Feb 01 '14 at 18:17

1 Answers1

0

The problem was that I used the Python folder as my working directory and named the script "random.py".

BBedit
  • 7,037
  • 7
  • 37
  • 50