-1

I'm trying to implement a simple SSTF program and trying to iterate over the queue of processes that have come in and subsequently delete anything that I have taken into account but it says "Out of index error".

for i in xrange(0,len(queue)):
    for j in xrange(0,(len(queue))):
        a = abs(queue[j]-initial_position)
        if(min>a):
            min = a
            pos = j

initial_position=queue[pos]

final_queue.append(initial_position)
del(queue[pos])

The complete error message:

initial_position=queue[pos]
IndexError: list index out of range

I'm really confused.

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
Nitya P
  • 1
  • 3
  • 3
    Please indent your code, we have no idea what's the scopes of anything in the posted code. – amit Sep 07 '16 at 06:32
  • 2
    "Use the debugger, Luke" is what Obi-wan would've said, had he been a developer. Quoting rules: "Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a **specific problem or error** and the shortest code necessary to reproduce it in the question itself. Questions without a **clear problem statement** are not useful to other readers. See: How to create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)." – code_dredd Sep 07 '16 at 06:32
  • Hope this is okay @amit – Nitya P Sep 07 '16 at 06:34
  • @NityaP I don't know, is the last two 2-3 indented correctly as well? Why so much empty lines? – amit Sep 07 '16 at 06:35
  • I made a "reasonable" attempt at fixing the messed up indentation. It has the same indent levels as the original code, removed some unnecessary parentheses, and added spaces between operators. Other than that, it's the same as original... – code_dredd Sep 07 '16 at 06:38
  • My guess (and it's only a guess, w/o more details and complete indentation fix): The posted code is not the original code, and the last 3 lines are infact in the outer loop, which means you delete while iterating. If this is the case, duplicate: http://stackoverflow.com/q/1207406/572670 – amit Sep 07 '16 at 06:39
  • @amit I will try that out! Thanks a ton – Nitya P Sep 07 '16 at 06:43
  • @ray Thank you. This is my first time posting on the forum. Sorry for all the editing that you had to do, appreciate the help. – Nitya P Sep 07 '16 at 06:44
  • is the indenting correct now? – Mixone Sep 07 '16 at 06:48
  • 1
    @NityaP, You should take a look at the [How to Ask](http://stackoverflow.com/help/how-to-ask) page. – code_dredd Sep 07 '16 at 06:50
  • @NityaP When i searched about SSTF , found some code samples it can use like reference http://pastebin.com/s7FxF69c. – Rahul K P Sep 07 '16 at 07:11
  • Your code as it is now does not run because you have 3 variables that are not defined. Please add relevant initializations. – Psytho Sep 07 '16 at 10:48

1 Answers1

1

My guess is that you didn't initialize min and pos properly (e.g. min is not small enough), thus the value pos has never been set. Then queue[pos] points to an undefined position.

Mouze
  • 54
  • 5