---------- Forwarded message ---------- Date: Thu, 14 Apr 2005 00:41:40 -0500 From: Jim and Laura Ahl <[EMAIL PROTECTED]> To: Danny Yoo <[EMAIL PROTECTED]> Subject: Re: [Tutor] Python backwards program
I thought you were on me a bit but I am so frustrated at this point. My instructor wrote and told me it was easy. So if it is easy why am I having so much trouble. I figured the other three programs by just reading the book and working on the computer. Does the 2.2 python have the ability to do this?
I think that the program will work with something like this in an expanded form.
The following gives me the last letter of the string.
backwords=raw_input("enter number or string:") print backwords[-1]
I have looked on a couple of sites and it says to use s.reverse() this does not work for some reason
Jim
Hi Jim,
do you know about the dir function? You can use it to show you all methods of an object. (Methods are, more or less, the things an object comes with support for doing to it. Ugly vagueness, but roughly right.)
The output of dir statements on my computer will be a bit different than yours, as I am using Python 2.4.1. But here is the result of doing dir on a string and a list:
>>> dir('a string')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> dir(['a', 'list'])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
>>> 'some string'.reverse() Traceback (most recent call last): File "<interactive input>", line 1, in ? AttributeError: 'str' object has no attribute 'reverse' >>>
And, that is what we would expect, as dir('a string') didn't show us any reverse method. *But* dir(['a', 'list']) did! So:
>>> my_list = ['this', 'is', 'my', 'list'] >>> my_list.reverse() >>> my_list ['list', 'my', 'is', 'this'] >>>
Does your version of Python have list.reverse()? (I've no idea when that got in, but I assume its been there since God was a lad.)
If so, one way you could solve your problem would be to take a string, convert it into a list, reverse the list, and then make a string out of the list again. To try that, you will want to combine some or all of the list and str builtin functions, and the string.split and string.join methods.[*] I'm still leaving work for you to do -- which pieces and how to combine them? Give it a try and show us what you've done.
[*] Really old Pythons do not have string methods at all. If yours is that old, we will have to suggest something else.
Run this:
>>> if 'join' in dir(str): print "My Python version has string methods" else: print "Oh, oh. I *really* need to upgrade my Python"
HTH,
Brian vdB
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor