问题
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