[Tutor] Program for outputing the letter backward

Hoffmann oasf2004 at yahoo.com
Wed Mar 29 08:03:00 CEST 2006


--- John Fouhy <john at fouhy.net> wrote:

> On 29/03/06, Hoffmann <oasf2004 at yahoo.com> wrote:
> > >>> vehicle='car'
> > >>> index = 0
> > >>> lenght =len(vehicle)
> > >>> last = vehicle[lenght -1]
> > >>> while last >= vehicle[0]:
> > ...      letter = vehicle[index]
> > ...      print letter
> > ...      last -= 1
> > ...
> 
> What is vehicle[index] ?
> 
> What if I change index, say,
> 
>    index = index + 1
> 
> Now what is vehicle[index] ?
> 
> --
> John.
> _______________________________________________

Hi John,

(1) vehicle[index] is: 'c'
(2) If index = index = 1, so vehicle[index] becomes:
'a'

I changed a bit more the code, but I am still in
trouble. please, take a look:

(1st. try):
>>> vehicle='car'
>>> index = vehicle[-1]        #'r'
>>> lenght =len(vehicle)       # 3 leters
>>> last = vehicle[lenght -1]  # 
>>> while last >= vehicle[0]:
...     letter = vehicle[index]
...     print letter
...     last -= 1
...
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
TypeError: string indices must be integers

(2nd, try):
>>> vehicle='car'
>>> index = vehicle[-1]
>>> while vehicle[-1] >= vehicle[0]:
...     letter = vehicle[index]
...     print letter
...     index -= 1
...
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
TypeError: string indices must be integers

Such a 'hard' simple problem...

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