[Tutor] Don't Understand Problem

Timo timomlists at gmail.com
Thu Feb 26 12:06:52 CET 2015


Op 26-02-15 om 09:55 schreef Alan Gauld:
> 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)
I think you missed your own solution here, Alan. You probably meant:

unvisited_caves = list(range(0, 20))

Timo

> ...
> 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
>



More information about the Tutor mailing list