Danielle
Danielle

Reputation: 55

TypeError: object of type 'builtin_function_or_method' has no len() while using string

I'm pretty sure my program has another stupid mistake, but I can't find it. I've tried to google it for a while, but so far no results.

I'm trying to use the len method for a loop. I've used it in exactly the same way in a different function in the program without problems, but in this function I get a TypeError:

def longestPalindrome(DNA):
    """
    Finds the longest palindrome in a piece of DNA.
    """
    DNA = DNA.upper #makes sure DNA is in all caps
    longest = ""

    for x in range(len(DNA)):
        for y in range(len(DNA)):
            long = DNA[x:y+1]
            if checkPalindrome(long) and (len(long) > len(longest)):
                longest = long           
    return longest

DNA is a string and checkPalindrome is an earlier function that checks whether a piece of DNA is a palindrome.

Upvotes: 1

Views: 853

Answers (2)

Craig
Craig

Reputation: 4855

Your line DNA = DNA.upper should be:

DNA = DNA.upper()

You've assigned the function DNA.upper to the variable DNA, which is why it is no longer a string.

Upvotes: 1

Arya McCarthy
Arya McCarthy

Reputation: 8829

DNA = DNA.upper()

Without parentheses, you are referring to the function called upper, but not executing it. DNA becomes the function, and it is no longer a string.

Upvotes: 1

Related Questions