Hi,I'm trying to figure out why my code is giving me an error. Any help is appreciated. Apologies if I am posting this in the wrong place. Very new to python so my code may seem novice to many of you.Thanks.

You seem to be confused about how for loops work. My recommendation (besides stranac's fabulous one) is that you revisit a tutorial on for loops, don't try to use a while loop, and don't try to use range() or xrange() either.

I can write this function as one simple line, a list comprehension with a sum() call at the end passing a generator to it. Without doing anything too fancy, I did it in 9 lines. (Ok, it was a little fancy but you should be able to do it in a couple more at the most.) My point is: if you find yourself writing a ton of code, you're overcomplicating things.

Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.

Now your code shouldn't be crashing anymore, BUT:It's still not working the way you want it to.The problem with your code is that your while loop never ends; this is due to a "fatal" mistake you made You increment value ONLY in your if-clause:

No. Noone should ever do that.I think that "should work for now"-style code isn't an objective of this forum.

Terrific answer.I'm sure Greggles' question is resolved by your helpful response, lacking neither quality nor depth.Just a side note:Why not elaborate why "noone should ever do that" and how your answer has helped anyone here?And if you're not too busy, why not give the asker a better alternative for his code?Thank you.

You should never iterate over the indexes, and instead over the elements themselves. To iterate over the indexes is error prone and harder to read. Typically, you don't need the index, and you do want the element, so iterating over the indexes is very misleading. When you do need the indexes, you can use enumerate()