### Thread: List index range:easy but I can't see it

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2005
Posts
55
Rep Power
11

#### List index range:easy but I can't see it

Greetings...

I'm stumped. I've ran this with a least 100 little changes and I can't figure out why I keep going out of range (and yes, Grim Archon, I have been doing my reading ). This uses a robot class that isn't published, so I don't know if anyone can help me by just looking at the syntax? This is the pertinent code I have, and what I get when I run it (please excuse the scaffolding):
More below....

direction=[N,W,E,S] #4 robot directions possible/scannable
#print direction
x,y= r.currentPosition()
#print x
#print y
xN,yN=r.sensor(N) #objects and how far away they are
xW,yW=r.sensor(W)
xE,yE=r.sensor(E)
xS,yS=r.sensor(S)
objlist=[(xN,yN), (xW,yW),(xE,yE),(xS,yS)]
for (i) in objlist:
m=objlist.index(min(objlist))
#print type (m)
#print min(objlist)[0]
#print min(objlist)[1]
if min(objlist)[0]==COIN:
for j in range(m): <--m is type 'int', m=2
print j
r.move(direction[m])
j=j+1

I figured this would work just like any other time I've used the range function, but it's not! Notice the j index keeps reinitializing itself to 0 (from the scaffolding):

0
1
0
1
0
1
Traceback (most recent call last):
File "C:\WINDOWS\Desktop\a8backup\samprobot.py", line 27, in ?
r.move(direction[m]) <--m is type 'int', m=2
File "robotlib.py", line 451, in move
RuntimeError: Past edge of board

I know this has to have a simple answer, but I can't find it. If anyone can point me in the right direction I sure would appreciate it!
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2005
Posts
37
Rep Power
11
When you use "for j in range(m)", you do not need to increment j, i.e. you don't need the j = j+1 line in there. Python takes care of that automatically. What is going on in your program is this...
The for j in range(m) line constructs a list that looks like this when m=2: [0,1].
j is 0 the first time through the loop, and prints as such.
You incrment j.
Python increments j (so it is now 2).
You are now out of your range, and the loop exits.

By the way, your code would be much easier to read if you used code tags.
hope this helps.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2005
Posts
55
Rep Power
11

#### The never ending loop

Actually I figured out what the main problem is: the line above the 'j range' line is a loop in the IF statement ABOVE it: the loop doesn't END (that's the problem), it keeps cycling through the IF statement:
"if min(objlist)[0]==COIN:"
I need to keep that 'if' statement and find a way to only loop through the indexing on the line below it. Any ideas? I know this is simple, but I've spent so long looking at it....
And I'm sorry but I don't know what code tags are.
B.

Originally Posted by Zone
When you use "for j in range(m)", you do not need to increment j, i.e. you don't need the j = j+1 line in there. Python takes care of that automatically. What is going on in your program is this...
The for j in range(m) line constructs a list that looks like this when m=2: [0,1].
j is 0 the first time through the loop, and prints as such.
You incrment j.
Python increments j (so it is now 2).
You are now out of your range, and the loop exits.

By the way, your code would be much easier to read if you used code tags.
hope this helps.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2005
Posts
37
Rep Power
11
Oops... I made a mistake when I said that python will re-increment j. It won't. It will just set j to the next item in the range, and disregard your j = j+ 1 statement. In other words, j=j+1 is not needed, but it is not your problem either.

Code tags are used to preserve indentation (crucial for python). You get them by using the little "wrap in code tags" button on the form for submitting a thread.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2005
Posts
55
Rep Power
11
I *finally* figured out what the problem was; thanks for pointing out the icons, I haven't even really noticed they were there.

Originally Posted by Zone
Oops... I made a mistake when I said that python will re-increment j. It won't. It will just set j to the next item in the range, and disregard your j = j+ 1 statement. In other words, j=j+1 is not needed, but it is not your problem either.

Code tags are used to preserve indentation (crucial for python). You get them by using the little "wrap in code tags" button on the form for submitting a thread.