Python Forum
Index Error, can't understand why. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Index Error, can't understand why. (/thread-7291.html)



Index Error, can't understand why. - Diamond - Jan-03-2018

Hello!
I am getting an IndexError, and not sure why? My goal is to print out the higher numeric list element.

def mag(var):
    pos=0
    mob=1
    while pos < len(var):
        if var[pos]<var[mob]: 
            pos = mob
            mob += 1
        else:
            mob += 1
    print pos
it returns this error:
Error:
line 5, in mag if var[pos]<var[mob]: IndexError: list index out of range
i have already tried to change the code several times but the result is the same, may you help me to understand the problem?
Thanks a lot!


RE: Index Error, can't understand why. - Gribouillis - Jan-03-2018

The value of mob may reach len(var), causing var[mob] to fail.


RE: Index Error, can't understand why. - j.crater - Jan-03-2018

Hello,
error says that your list doesn't contain the element you want to access. It may only contain one element (index 0). So var[mob] (which is var[1] tries to access the second element of var list, which doesn't exist.


RE: Index Error, can't understand why. - Diamond - Jan-03-2018

(Jan-03-2018, 10:21 AM)j.crater Wrote: Hello,
error says that your list doesn't contain the element you want to access. It may only contain one element (index 0). So var[mob] (which is var[1] tries to access the second element of var list, which doesn't exist.

Thanks for the answer, and for example :

var=[1,2,3,2]
now the var[1] exists, why the error is still happening if the second element of the list exists


RE: Index Error, can't understand why. - buran - Jan-03-2018

def mag(var):
    pos=0
    mob=1
    while pos < len(var):
        print pos, mob
        if var[pos]<var[mob]: 
            pos = mob
            mob += 1
        else:
            mob += 1
    print pos
    
mag([1,2,3,4])
Output:
0 1 1 2 2 3 3 4 Traceback (most recent call last): File "C:\Users\BKolev\Desktop\hw.py", line 13, in <module> mag([1,2,3,4]) File "C:\Users\BKolev\Desktop\hw.py", line 6, in mag if var[pos]<var[mob]: IndexError: list index out of range >>>
as you can see before the error mob is already equal to 4, however var has only 4 elements (i.e. indexes are 0 to 3), so there is no element with index 4 that you try to access via var[mob]


RE: Index Error, can't understand why. - Diamond - Jan-03-2018

(Jan-03-2018, 11:02 AM)buran Wrote:
def mag(var):
    pos=0
    mob=1
    while pos < len(var):
        print pos, mob
        if var[pos]<var[mob]: 
            pos = mob
            mob += 1
        else:
            mob += 1
    print pos
    
mag([1,2,3,4])
Output:
0 1 1 2 2 3 3 4 Traceback (most recent call last): File "C:\Users\BKolev\Desktop\hw.py", line 13, in <module> mag([1,2,3,4]) File "C:\Users\BKolev\Desktop\hw.py", line 6, in mag if var[pos]<var[mob]: IndexError: list index out of range >>>
as you can see before the error mob is already equal to 4, however var has only 4 elements (i.e. indexes are 0 to 3), so there is no element with index 4 that you try to access via var[mob]

Thanks for the answer, its very clear now!