8 Replies - 1151 Views - Last Post: 26 September 2012 - 10:03 PM Rate Topic: -----

#1 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

index out of range error

Posted 26 September 2012 - 11:01 AM

My code is below. It works but then python tells me it is going out of range.

def fun3():
    count = 0
    myList = list (range(5))
    while count < len(mylist)+1:
        count = count+1
        print (myList[count])


Is This A Good Question/Topic? 0
  • +

Replies To: index out of range error

#2 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: index out of range error

Posted 26 September 2012 - 11:08 AM

Get rid of the '+1'. If there are N elements, they will be numbered 0..N-1, which is what the loop will generate without the +1. Hope this helps.

To clarify, I mean the '+1' in the condition of the while loop, not the one incrementing 'count'. Sorry, haven't had my coffee yet :P.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,201
  • Joined: 19-March 11

Re: index out of range error

Posted 26 September 2012 - 11:09 AM

def fun3():
    myList = list (range(5))  # iterate over items 0 through 4? okay...
    for i in myList:
       print i


Or else:

def fun4():
    for i in range(5):
       print i

This post has been edited by jon.kiparsky: 26 September 2012 - 11:10 AM

Was This Post Helpful? 1
  • +
  • -

#4 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: index out of range error

Posted 26 September 2012 - 11:10 AM

Thank you very much for your suggestion but are you talking about the +1 from count or on the while loop.

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: index out of range error

Posted 26 September 2012 - 11:19 AM

It still gives me an error that it is out of range and now its only printing 1-4 and not 0-4
Was This Post Helpful? 0
  • +
  • -

#6 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: index out of range error

Posted 26 September 2012 - 11:29 AM

Are you there?
Was This Post Helpful? 0
  • +
  • -

#7 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: index out of range error

Posted 26 September 2012 - 11:37 AM

View Postkehara15, on 26 September 2012 - 11:19 AM, said:

It still gives me an error that it is out of range and now its only printing 1-4 and not 0-4


Increment count after you do the print statement. Otherwise, the first value of count will be '1' instead of '0'.

jon.kiparsky solution 'for..in' etc. is also quite valid, and maybe a bit easier to understand.
Was This Post Helpful? 0
  • +
  • -

#8 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: index out of range error

Posted 26 September 2012 - 11:41 AM

thank you.

let me try that.
i dont think they want a for loop. they want me to keep the while loop

It works great now.
Thank you
Was This Post Helpful? 0
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: index out of range error

Posted 26 September 2012 - 10:03 PM

Please don't name posts "Error in code". You're at a programming support website, we know your code has errors in it. 75% of the posts could be labeled as "My Python Program has an error" but if we did that, what would be the point in giving them a title at all? How about instead we call this post "index out of range error".

EDIT:
Couldn't you achieve the same with?
def fun3():
    count = 0
    size = 5
    while count < size:
        count = count+1
        print (count)

This prints out 0 - 5 using a while loop but without creating lists and indexing and whatnot

This post has been edited by atraub: 26 September 2012 - 10:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1