[Tutor] What am I missing on the index?
DavidCraig@PIA.CA.GOV
DavidCraig@PIA.CA.GOV
Mon, 25 Jun 2001 12:29:00 -0700
Sorry to bother you all again. I have been trying this one all weekend.
What am I doing wrong. This is an exercise from How to Think Like a
Computer Scientist (Python):
The exercise is to write a function that takes a string as an argument that
and outputs the letters backwards, one per line.
## stringloop function prints a string backwards
def stringback(string):
string = "David"
index = 0
while index < len(string):
letter = string[index]
print letter
index = index - 1
And my output back is:
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>>
stringback(string)
D
d
i
v
a
D
Traceback (most recent call last):
File "<pyshell#0>", line 1, in ?
stringback(string)
File "C:/Python21/Practice/stringback.py", line 7, in stringback
letter = string[index]
IndexError: string index out of range
Any help would be appreciated. I just can't seem to see it.
Thanks , Dave
D. H. Craig, CSM