AttributeError: 'module' object has no attribute 'lower'

##def keySearch(L, k):
##    for elem in L:
##        if elem[0] == k: return elem[1]
##    return None
##
##EtoF = {'bread': 'du pain', 'wine': 'du vin',\
##        'eats': 'mange', 'drinks': 'bois',\
##        'likes': 'aime', 1: 'un',\
##        '6.00':'6.00'}
##
##def translateWord(word, dictionary):
##    if word in dictionary:
##        return dictionary[word]
##    else:
##        return word
##
##def translate(sentence):
##    translation = ''
##    word = ''
##    for c in sentence:
##        if c != ' ':
##            word = word + c
##        else:
##            translation = translation + ' '\
##                          + translateWord(word, EtoF)
##            word = ''
##    return translation[1:] + ' ' + translateWord(word, EtoF)
##
##print (translate('John eats bread'))
##print (translate('Eric drinks wine'))
##print (translate('Everyone likes 6.00'))

def toChars(s):
    import string
    s = string.lower(s)
    ans = ''
    for c in s:
        if c in string.lowercase:
            ans = ans + c
    return ans

def isPal(s):
    if len(s) <= 1:
        return True
    else:
        return s[0] == s[-1] and isPal(s[1:-1])

def isPalindrome(s):
    """Returns True if s is a palindrome and False otherwise"""
    return isPal(toChars(s))

print (isPalindrome('Guttag'))
print (isPalindrome('Guttug'))
print (isPalindrome('Able was I ere I saw Elba'))
print (isPalindrome('Are we not drawn onward, we few, drawn onward to new era?'))
##
##def fib(x):
##    """assumes x an int >= 0
##        Returns Fibonacci of x"""
##    assert type(x) == int and x >=  0
##    if x == 0 or x == 1:
##        return 1
##    else:
##        return fib(x-1) + fib(x-2)
##
##def testFib(n):
##    for i in range(n+1):
##        print ('fib of', i, '=', fib(i))

Open in new window



Message      File Name      Line      Position      
Traceback                        
    <module>      C:\Users\Acer\Documents\portable-python\myfiles\6-all-edit.py      52            
    isPalindrome      C:\Users\Acer\Documents\portable-python\myfiles\6-all-edit.py      50            
    toChars      C:\Users\Acer\Documents\portable-python\myfiles\6-all-edit.py      35            
AttributeError: 'module' object has no attribute 'lower'                        


using portable python 3.2 for windows
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peprCommented:
The truth is exactly as reported. The string module has no function like lower(). Actually, the string module were more important in early Python days. The module is not marked as deprecated, but I would recommend to avoid using it if you can do the same using alternative approach. The reason is that the code should be as simple as possible. Using another module brings dependencies and lesser the clarity. Also, the next error would be "there is no 'lowercase' attribute". You should or use string.ascii_lowercase (https://docs.python.org/3.4/library/string.html) or just explicit string literal.

Try to replace your function like that:
def toChars(s):
    s = s.lower()
    ans = ''
    for c in s:
        if c in 'abcdefghijklmnopqrstuvwxyz':
            ans = ans + c
    return ans

Open in new window

The .lower() is now the method of all strings.

A side note, the + applied to the strings is rather expensive operation. Recall that strings are immutable (Python illustrated (part 3)). When using ans = ans + c, you are using two string objects on the right side of the assignment, then creating the third object assigned to the left side variable, and throwing the two away. But it is for later lessons. Your code is OK for the first examples.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
s.lower
and
s

thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.