[Tutor] Program for outputing the letter backward
Kent Johnson
kent37 at tds.net
Wed Mar 29 05:24:11 CEST 2006
Hoffmann wrote:
> Hello:
>
> I am trying to write a code (this is an exercose from
> a book). The goal is to write a program that takes a
> 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 that
> takes the string 'car' as the input:
>
> vehicle='car'
> index = vehicle[-1] #the last letter
> index_zero = vehicle[0] #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
letter=vehicle[index]
decause index is a letter. It's important to give us accurate
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
problem.
Kent
More information about the Tutor
mailing list