[Tutor] Working with strings

Pedro Diaz Jimenez pdiaz88@terra.es
Mon, 12 Mar 2001 04:21:39 +0100


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

what about this?

>>> def backword(s):
...     index = len(s)-1
...     while index >= 0:
...             print s[index],
...             index = index-1
...
>>> backword("hola")
a l o h
>>>

Does anyone know how to take portions of a list. I would like to  resolve 
this problem this in a recursive manner, but given a="potato" I don't know 
how to take "potat" without writting another function (which would destroy 
the beauty of a well designed recursive function ). Using python 1.5.2 (yeah, 
its old but works great :)


Cheers
Pedro


On Monday 12 March 2001 15:41, Paul Jones wrote:
> Hi- I'm going through the OBP tutorial on how to think like a scientist by
> Allen B. Downey and Jeffrey Elkner (Ch7). There is a problem on doing a
> program to write a function to accept a string -and output it reversed. ie
> Bruce --->ecurB I'm using the IDLE interface on Win98 (I have Linux but my
> modem is not working so well on it at the moment). Can you correct my
> program below and give me some sample program as to how to do it simply.
> I'm not having trouble with the logic of the string manipulation(I think) -
> just getting the program to accept input and print output. Regards..
> Paul Jones
>
Watch your indentation
> >>> def backword(bruce):
Whats the role of the bruce param??. I'll use either params or local vars 
with raw_input
>
>  bruce=raw_input('input a word to reverse  ')
>  index=len(bruce)-1
>  while index<0:
wrong^
>   letter=bruce[index]
>   print letter,
>   index = index+1
wrong  ^

>
> >>> backword
>
> <function backword at 00BC015C>
>
> >>> backword(flowerpot)
>
> Traceback (innermost last):
>   File "<pyshell#36>", line 1, in ?
>     backword(flowerpot)
> NameError: There is no variable named 'flowerpot'
>
> >>> backword('flowerpot')
>
> input a word to reverse  flowerpot
>
> >>> print bruce
>
> harold # a value I entered earlier that it hasn't cleared.
>
> >>> def backword('bruce'):
>
> SyntaxError: invalid syntax
>
> >>> backword(flowerpot)
>
> Traceback (innermost last):
>   File "<pyshell#40>", line 1, in ?
>     backword(flowerpot)
> NameError: There is no variable named 'flowerpot'
>
> email: paulnsharon@dingoblue.net.au

- ----------------------------------------
Content-Type: text/html; charset="iso-8859-1"; name="Attachment: 1"
Content-Transfer-Encoding: quoted-printable
Content-Description: 
- ----------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.1 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE6rEDDnu53feEYxlERAi64AJ4xxBoUAITH4ztgRYt8uqRrz6iV8gCgvqoq
iHYK8WW12sOD01V62OzHMn0=
=Umi7
-----END PGP SIGNATURE-----