Jan-03-2018, 11:45 AM
(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])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
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 >>>var[mob]
Thanks for the answer, its very clear now!