【问题标题】:TypeError: object of type 'builtin_function_or_method' has no len() while using stringTypeError: 'builtin_function_or_method' 类型的对象在使用字符串时没有 len()
【发布时间】:2020-08-02 19:13:56
【问题描述】:

我很确定我的程序还有另一个愚蠢的错误,但我找不到。 google了一阵子,还是没有结果。

我正在尝试使用 len 方法进行循环。我在程序中的不同函数中以完全相同的方式使用它没有问题,但在这个函数中我得到一个 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 是一个字符串,而 checkPalindrome 是一个较早的函数,用于检查一段 DNA 是否为回文。

【问题讨论】:

    标签: python typeerror string-length


    【解决方案1】:
    DNA = DNA.upper()
    

    如果没有括号,您指的是名为upper 的函数,但没有执行它。 DNA变成了函数,不再是字符串了。

    【讨论】:

      【解决方案2】:

      你的DNA = DNA.upper应该是:

      DNA = DNA.upper()
      

      您已将函数DNA.upper 分配给变量DNA,这就是它不再是字符串的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-22
        • 2021-02-26
        • 2016-02-15
        • 1970-01-01
        • 2015-08-21
        • 1970-01-01
        • 2019-05-23
        • 2013-04-18
        相关资源
        最近更新 更多