[Tutor] UselessPython 2.0

Dick Moores rdm at rcblue.com
Tue Apr 12 12:08:48 CEST 2005


Michael Janssen wrote at 10:30 4/11/2005:
>On Apr 10, 2005 7:18 AM, Dick Moores <rdm at rcblue.com> wrote:
>
> > I'm a newbie, but would this qualify as a contribution to 
> UselessPython 2.0?
>
>Hello Dick,
>
>don't be shy, or do you suspect it might be too usefull? ;-) I found
>it funny, so it must be good enough.
>
>here my remarks:
>
> > def makeStringAllLowercaseAlpha(s):
> >     """
> >     Take any string, convert all uppercase alphabetic characters to
> > lower case,
> >     then strip all non-alphabetic characters
>
>[what's bad about non-alphabetic characters?]
>
> >     """
> >     s1 = string.lower(userstring)
>
>oops: this function gets an argument s, here you're using the global
>variable userstring. I know, it works, but there will be functions
>were mistakes like this weren't just ugly but buggy.
>
> >     s2 = ""
> >     for index in range(len(s1)):
> >         if s1[index] in string.ascii_lowercase:
> >             s2 += s1[index]
>
>or easier (much more readable, helps understanding the programm):
>
>for char in s1:
>      if char in string.ascii_lowercase:
>            s2 += char
>
>regards
>Michael

Thanks very much, Michael. Here's the revision.
=====================================
# isPalindrome.py

# revision of http://www.uselesspython.com/glpalindrome.py to handle 
palindromes
#   such as "Radar" and "A man, a plan, a canal, Panama!"

# http://www.palindromelist.com/ has many palindromes

# string.ascii_lowercase is 'abcdefghijklmnopqrstuvwxyz'

import string

def isPalindrome(w):
     return w == '' or (w[0]==w[-1]) and isPalindrome(w[1:-1]) # recursive

def makeStringAllLowercaseAlpha(s):
     """
     Take any string, convert all uppercase alphabetic characters to 
lower case,
     then strip all non-alphabetic characters
     """
     s1 = string.lower(s)
     s2 = ""
     for char in s1:
         if char in string.ascii_lowercase:
             s2 += char
     return s2

userstring = raw_input('Enter a word or sentence to test: ')
userstringRevised = makeStringAllLowercaseAlpha(userstring)
====================================

Dick




More information about the Tutor mailing list