I am practicing with "Think Python", Exercise 8.1 that:
"Write a function that takes a string as an argument and displays the letters backward, one per line."
I am able to do this question, by using banana as an example to print each letter per line.
index = 0
fruit = "banana"
while index < len(fruit):
letter = fruit[len(fruit)-index-1]
print letter
index = index + 1
However, I would like to generalize the situation to any input words and I got the problem, my code is
index = 0
def apple(fruit):
while index < len(fruit):
letter = fruit[len(fruit)-index-1]
print letter
index = index + 1
apple('banana')
The corresponding errors are:
Traceback (most recent call last):
File "exercise8.1_mod.py", line 21, in <module>
apple('banana')
File "exercise8.1_mod.py", line 16, in apple
while index < len(fruit):
UnboundLocalError: local variable 'index' referenced before assignment
I think there should be problems concerned with the function arguments used. Any helps will be appreciated.