• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2863
  • Last Modified:

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
0
rgb192
Asked:
rgb192
1 Solution
 
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
 
rgb192Author Commented:
s.lower
and
s

thanks
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now