john boy wrote: > using the following program: > > fruit = "banana" > index = 0 > while index < len (fruit): > letter = fruit[index-1] > print letter > index= index -1 > > this program is supposed to spell "banana" backwards and in a vertical > patern...it does this....but after spelling "banana" it gives an error > message: > refering to line 4: letter = fruit[index-1] > states that it is an IndexError and the string index is out of range > Anybody know how to fix this? > Change the termination condition on your loop. If you print out the values of index as the loop goes round you'll see you are printing characters whose index values are -1, -2, ..., -6
Unfortunately -7 is less than -6, so your loop keeps on going, with the results you observe. Note, however, that there are more pythonic ways to perform this task. You might try: for index in range(len(fruit)): letter = fruit[-index-1] print letter as one example. I'm sure other readers will have their own ways to do this, many of them more elegant. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.python.org/pycon/ -- http://mail.python.org/mailman/listinfo/python-list