[Tutor] Program for outputing the letter backward
Hoffmann
oasf2004 at yahoo.com
Wed Mar 29 07:19:45 CEST 2006
--- John Fouhy <john at fouhy.net> wrote:
> On 29/03/06, Hoffmann <oasf2004 at yahoo.com> wrote:
> > 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
> >
> > The problem is that I get no output here. Could I
> hear
> > from you?
>
> I can print the letters backwards like this:
>
> vehicle = 'car'
> print vehicle[2]
> print vehicle[1]
> print vehicle[0]
>
> Output:
>
> r
> a
> c
>
> -----
>
> This is not very useful, though, because it will
> only work for strings
> that are exactly three letters long. Can you see
> how to write a loop
> to produe this output?
>
> Hint: the len() function will tell you how long a
> string is.
>
> eg: if vehicle == 'car' then len(vehicle) == 3.
>
> --
> John.
> _______________________________________________
Hi John,
I am still 'blind' here.
Please, see below what I did, and what I got:
>>> vehicle='car'
>>> index = 0
>>> lenght =len(vehicle)
>>> last = vehicle[lenght -1]
>>> while last >= vehicle[0]:
... letter = vehicle[index]
... print letter
... last -= 1
...
c
Traceback (most recent call last):
File "<stdin>", line 4, in ?
TypeError: unsupported operand type(s) for -=: 'str'
and 'int'
As you can see, I am still a newbie...
Could anyone, please, guide me on this exercise?
Thanks!
Hoffmann
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
More information about the Tutor
mailing list