1

I've been trying to fix this but the same error message came up every time:

while number_list[i] <= number_list[j]:

IndexError: list index out of range

I've searched for the same type of bug, but not very similar cases found.

Here it is the head program (orders the numbers of my list, from the little one to the bigger):

    number_list=[]

list_lenght=int(input("List lenght: "))

while len(number_list)<list_lenght:
    item=input("Enter new item to the list:")
    number_list.append(item)
    print(number_list)

print("That's your number list: ",number_list)

number_list_final=[]

def order_number_list(number_list):
    i=0
    j=1
    while (j)<=(len(number_list)-1):
        while number_list[i]<=number_list[j]:
            j=j+1
        i=j
        j=i+1
    final_item=number_list[i]
    number_list_final.append(final_item)`
    del number_list[i] 
    order_number_list(number_list)

order_number_list(number_list)
print(number_list_final)

I know this is about iterating with the list while modifying it, but no idea how to fix it.

Can anyone help me to debug this, or give me some tips?

Thank you!

Gonzalo Molina
  • 15
  • 1
  • 1
  • 6

3 Answers3

1
number_list=[]

list_lenght=int(input("List length: "))

while len(number_list)<list_lenght:
    item=input("Enter new item to the list:")
    number_list.append(int(item))
    print(number_list)

print("That's your number list: ",number_list)

number_list_final=[]

def order_number_list(number_list):
    current_low = ["number"]
    current_low[0] = number_list[0]
    x = 1
    current_low_pos = 0
    while x < len(number_list):
        if current_low[0] > number_list[x]:
            current_low[0] = number_list[x]
            current_low_pos = x
        x = x + 1
    del number_list[current_low_pos]

    if number_list == []:
        remaining_list = []
    else:
        remaining_list = order_number_list(number_list)

    return (current_low + remaining_list)
number_list_final = order_number_list(number_list)
print(number_list_final)

This is code that has been clarified and corrected. j was not working right, as other answers have pointed out.

number_list.append(item)

needed to be changed to:

number_list.append(int(item))

because you cant compare strings with the '<' operator.

I also added a return statement, added this line of code:

number_list_final = order_number_list(number_list)

because your final print statement would print an empty list because:

order_number_list(number_list)

doesn't change the variable number_list_final.

I changed some of the function code to simply and make it more readable.

It also explicitly sets the current lowest item to be the first, and changes that if a lower item is found later on.

Hope this helps, let me know if you have any questions!

ANVGJSENGRDG
  • 153
  • 1
  • 8
  • Finally! Thak you, i will chek the new lines and compare with mine. What I am not able to understand is: why should I use: number_list.append(int(item)) if I've introduced the list inputs with: int(input()) ? – Gonzalo Molina Jan 18 '17 at 17:54
  • @GonzaloMolina you only used int(input()) on the input for the list length, not for each individual item in the list. look back at the origional code in the question, the only use of int() on an input was for the length of the list, not each item in the list. – ANVGJSENGRDG Jan 18 '17 at 18:41
  • Thank you very much and sorry for disturbing you with such easy doubts. only few reminding doubts: why do we put "numbers" inside --> current_flow=[ ] ? and i don't understand the las **if** and **else** estatements. – Gonzalo Molina Jan 18 '17 at 19:14
  • @GonzaloMolina thats poorly written, it should be blank as you suggested, and the following line current_low[0] = number_list[0] should be changed to current_low.append(number_list[0]) I did that so I could assign it, rather than append it. – ANVGJSENGRDG Jan 18 '17 at 19:21
  • 1
    Once again, thanks a lot! Should exist more people like you, with patience for those who are starting with programming. ;) – Gonzalo Molina Jan 18 '17 at 19:24
0

If I understand you correctly, you're trying to sort a list of numbers. There's a built-in Python method for this, that has been optimized for ages to be fast and use minimal memory, it's called sorted.

Instead of running order_number_list, try:

print(sorted(number_list))

If this is some kind of homework, and you're not allowed to use the built-in sorted, for educational purposes I would recommend you to use a sorting algorithm called bubble sort, more information here - Bubble Sort Homework.

Community
  • 1
  • 1
Niklas9
  • 8,816
  • 8
  • 37
  • 60
  • I imagine that this is a homework assignment for OP in which they cannot use built-ins like `sorted`. – blacksite Jan 18 '17 at 17:20
  • I know about the existence o built-ins, but as i am starting with Python, I would rather prefer to develop the program step by step. I want to order a list of numbers from the little one to the bigger. I introduce manually the list, and then I evaluate it with the **order_number_list** function. So, first I make **while** loop in order to compare each index with each one to the last. Then, when one is recognized as the little one, i delete it from the original list with the **del** statement and I store it in the new list with **append**. – Gonzalo Molina Jan 18 '17 at 17:32
  • @GonzaloMolina that's not a very efficient sorting algorithm, and would likely be less buggy if you used for-loops. `while` loops are generally used sparingly, if you want to learn how Python is typically used. – juanpa.arrivillaga Jan 18 '17 at 18:08
  • Yeah! I know for-loops are better than while ones, but wise, i was testing how tricky could it be. thak you all, really... – Gonzalo Molina Jan 18 '17 at 18:10
0

Updating your code to include a couple of print statements shows that the j increases to past the length of the list. Then the "while number_list[i]<=number_list[j]:" no longer can be completed as there is no "number_list[j]" for the j value.

number_list=[1,2,3,4,5]

list_lenght=5
#=int(input("List lenght: "))

#while len(number_list)<list_lenght:
#    item=input("Enter new item to the list:")
#    number_list.append(item)
#    print(number_list)

print("That's your number list: ",number_list)

number_list_final=[]

def order_number_list(number_list):
    i=0
    j=1
    while (j)<=(len(number_list)):
        while number_list[i]<=number_list[j]:
            print (j)
            print (i)
            j=j+1
        i=j
        j=i+1
    final_item=number_list[i]
    number_list_final.append(final_item)
    del number_list[i] 
    order_number_list(number_list)

order_number_list(number_list)
print(number_list_final)

This outputs :

That's your number list:  [1, 2, 3, 4, 5]
1
0
2
0
3
0
4
0
Traceback (most recent call last):
  File "C:/Python3/numberlist.py", line 30, in <module>
    order_number_list(number_list)
  File "C:/Python3/numberlist.py", line 19, in order_number_list
    while number_list[i]<=number_list[j]:
IndexError: list index out of range

The code is falling over once j is 5 for this example, it is trying to compare to a number that doesnt exist

CodeCupboard
  • 1,507
  • 3
  • 17
  • 26
  • I am not understanding at all what you mean but, if you have a list with 5 numbers, list[5] exists. and since i am using **del** to modify the old list, the new one will be evaluated with his new lenght. – Gonzalo Molina Jan 18 '17 at 17:41
  • python labels start at 0. The first item in the list python calls 0. In the above example, if you print (number_list[j]) when j = 0, it will print "1". – CodeCupboard Jan 18 '17 at 17:42
  • I know how the list labels work, but with this affirmation you are saying nothing to me. But wise, thank you. – Gonzalo Molina Jan 18 '17 at 17:49