[Tutor] Program for outputing the letter backward
oasf2004 at yahoo.com
Wed Mar 29 05:29:42 CEST 2006
--- Kent Johnson <kent37 at tds.net> wrote:
> Hoffmann wrote:
> > Hello:
> > I am trying to write a code (this is an exercose
> > a book). The goal is to write a program that takes
> > string and outputs the letters backward, ine per
> > line.
> > Ok. I did a test first, by writing a code with
> > numbers:
> > a=0; b=10
> > while a<=b:
> > print b
> > b -= 1
> > Here the output is:
> > 10
> > 9
> > 8
> > 7
> > 6
> > 5
> > 4
> > 3
> > 2
> > 1
> > 0
> > That worked fine.
> > Now, back to my exercise. I tried to write a code
> > takes the string 'car' as the input:
> > vehicle='car'
> > index = vehicle[-1] #the last letter
> > index_zero = vehicle #the first letter
> > while index >= index_zero:
> > letter=vehicle[index]
> > print letter
> > index -= 1
> You are confusing the index of a letter - the number
> which represents
> its position in the word - with the letter itself.
> In your code, index
> and index_zero are actually letters, not indices.
> Try to rewrite the
> code so they are numbers.
> > The problem is that I get no output here.
> My guess is you got a TypeError on the line
> decause index is a letter. It's important to give us
> descriptions of what happens, and to show error
> messages and the
> tracebacks that come with them. This can be very
> helpful when you have a
Sorry for not showing the traceback the first time.
Please, see it below:
Traceback (most recent call last):
File "<stdin>", line 2, in ?
TypeError: string indices must be integers
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
More information about the Tutor