--- Ed Singleton <[EMAIL PROTECTED]> wrote: > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote: > > --- John Fouhy <[EMAIL PROTECTED]> wrote: > > > > > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> > 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 > > A technique I used to find useful when I was very > first learning (and > struggling) was to calculate the variables for each > pass of the loop > (basically remove all the variable names, just like > doing algebra). > > So: > > >>> vehicle='car' > >>> index = 0 > >>> lenght = len(vehicle) # therefore: > >>> lenght = 3 > >>> last = vehicle[lenght -1] # therefore: > >>> last = vehicle[2] # therefore: > >>> last = "r" > >>> while "r" >= "c": # first pass > ... letter = vehicle[index] # therefore: > ... letter = vehicle[0] # therefore: > ... letter = "c" > ... print letter > ... last -= 1 # therefore: > ... "r" -= 1 # therefore: > ... "r" = "r" - 1 # therefore: > ... ERROR > > You'll find that that can make it much clearer what > is actually > happening. An alternative is to use lots and lots > of print > statements: > > >>> vehicle='car' > >>> print vehicle > >>> index = 0 > >>> print index > >>> lenght = len(vehicle) > >>> print lenght > > and so on... > > It would be really good if there was a way to have a > "verbose" > interpreter that showed you each of the steps in the > process, but I > don't know of any. > > For example: > > >>> last = vehicle[lenght -1] > last = vehicle[2] > last = "r" > >>> > > Ed >
Hi Ed, Many thanks for the hints! Hoffmann __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor