def fun3():
count = 0
myList = list (range(5))
while count < len(mylist)+1:
count = count+1
print (myList[count])
index out of range error
Page 1 of 18 Replies - 520 Views - Last Post: 26 September 2012 - 10:03 PM
#1
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.
Replies To: index out of range error
#2
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
.
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
#3
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
#4
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
Thanks
#5
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
#7
Re: index out of range error
Posted 26 September 2012 - 11:37 AM
kehara15, 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.
#8
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
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
#9
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?
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|