I have a very simple piece of code in Python 2.7 where I must use a while loop
to traverse an index backwards. I am getting a print out where it goes backwards, but my while loop
does not stop at the end and therefore produces an out of range error and I do not know why. I am trying to dope it out but failing.
Here is my code:
fruit = 'banana'
print len(fruit)
index = 0
while index <= len(fruit):
letter = fruit[index - 1]
print letter
index = index - 1
What I think is going on here is I am initializing the var index
to 0
and then asking python to work with the var fruit
while the index is less than or equal to the size of fruit. The problem is when index gets to 0, I have also tried using just < but the way I wrote the code it seems that it still goes beyond 0, I am not sure though.