On Apr 10, 2005 7:18 AM, Dick Moores <[EMAIL PROTECTED]> 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
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor