Python Forum
Index Error, can't understand why.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Index Error, can't understand why.
#1
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!
Reply
#2
The value of mob may reach len(var), causing var[mob] to fail.
Reply
#3
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.
Reply
#4
(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
Reply
#5
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]
Reply
#6
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 980 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 766 May-15-2023, 03:28 PM
Last Post: buran
  Error I don't understand finndude 2 5,096 Oct-12-2022, 02:43 PM
Last Post: finndude
  I'm getting a String index out of range error debian77 7 2,349 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  Python Error List Index Out of Range abhi1vaishnav 3 2,309 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  Index error - columns vs non-column Vinny 3 4,919 Aug-09-2021, 04:46 PM
Last Post: snippsat
  How to resolve Index Error in my code? codify110 6 3,023 May-22-2021, 11:04 AM
Last Post: supuflounder
  index error surim 4 2,514 Dec-05-2020, 02:34 PM
Last Post: deanhystad
  I have an index error inline 76 but I write the program in a way that cant reach tha abbaszandi 2 2,064 Nov-13-2020, 07:43 AM
Last Post: buran
  when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 5,016 Nov-10-2020, 08:46 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020