[Tutor] Newbie python programmer here.

Michael Janssen Janssen@rz.uni-frankfurt.de
Sun May 18 16:24:02 2003


On Sun, 18 May 2003, TERRENCE WALKER wrote:

> Hi everyone, I just started programming in python and just started

Hi Jarrod,

nice to meet you.

> subscribing to this mailing list as well. I look forward to gathering
> and sharing ideas and learning too. Anyhow, here is my first question.
> I have a function that is supposed to reverse all the letters in a word.
> That is what I want it to do anyhow. I can't seem to get it right. What
> am I doing wrong?

A good exercise for the beginner (because it has to do with for-loops and
builtins datatypes). Since the standard-library has already a function to
do this (string.swapcase) it would be nonsense to recode it comlpetly for
you (that means take you the possibility to learn). So I give just some
hints and you're free to chat with us about further problems, experiences:

1. name = "aname": Its overwrites "name" in any case and you can't give
another "name" as a parameter to the function (I assume this was clear
to you and meant for testing).

2. "print" automatically append newline-character. This is why the output
is scattered over severall lines. Even surpressing the newline with a
trailing comma ("print i,") doesn't help much, because print continues to
seperate the output via spaces.

Solutions are: write directly to "standart out" via sys.stdout.write(i)
(This means: use the "write" method of the "file-like-object" stdout
provideed by the "sys modul) or gather the characters into a new
helper-string and print it out as whole:

for i in name:
    helper = ""
    helper = "" + i
print helper

3. i[-1] (take the last character from a sequence of characters) is
needless, because i is set to a single character within the for loop.

4. You need some logic to turn the case (of course ;-). Look out for
"isupper" and "islower" string-methods:
{Python-Doc}/lib/string-methods.html

Michael


>
> def reverse(name):
>     name = "aname"
>     for i in name:
>            print i[-1]
>
> I know this is trivial to you more experienced people. But I really want
> to learn this language. Thanks for the help.
>
> Jarrod
> p.s. this mail will always have the name Terrence on it my name is Jarrod.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>