[Tutor] Don't Understand Problem

Alan Gauld alan.gauld at btinternet.com
Thu Feb 26 09:55:55 CET 2015


On 26/02/15 04:30, kcberry wrote:
> So I am new to this, and I have a book call /Hello Python by Anthony Briggs/.
> It is a good book but it is using Python 2 I think and I can't get my code
> to work. I get an "AttributeError: 'range' object has no attribute
> 'remove'". I have tried to look it up on the web with no luck. Can someone
> help me understand this .

Please always send full error traces, they contain a lot of useful details.

Meantime in this case we can figure it out...

> #Setting up cave network
> unvisited_caves = range(0,20)
> current = 0
> visited_caves = [0]
> *unvisited_caves.remove(0)*

In Python 3 eange does not return a list. It returns something called a 
"range object" which helps save memory for large daya sets.
If you need to use it as a list you need to explicitly
convert it using list():

unvisited_caves = range(0,20)
...
unvisited_caves.remove(0)

However remove() may not do what you think. It removes the
value zero from your list not the first element. I this case they are 
the same but in other cases they might not be. You may want to use del() 
instead

del(unvisited_caves[0])

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list