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

假装没事ソ 提交于 2020-08-11 11:47:01

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/43577399/typeerror-object-of-type-builtin-function-or-method-has-no-len-while-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!