I'm on chapter 2 of the book Impractical Python Projects and I'm working on the code that is supposed to go through a dictionary list, find the palindromes in the list, and print them out in a new list. Here's the code.
import load_dictionary
word_list= load_dictionary.load('2of4brif.txt')
pali_list= []
for word in word_list:
if len(word) > 1 and word == word[::-1]:
pali_list.append(word)
print("\nNumber of palindromes found = {}\n".format(len(pali_list)))
print(*pali_list, sep='\n')
I'm having problems wit the if statement with the len() function. When I try to run the code it keeps returning this error:
TypeError: object of type 'builtin_function_or_method' has no len()
I tried changing the name 'word' to something else, thinking that maybe it was the built-in funtion that was being talked about, but the result is the same for each word or letter that I tried to use
ADDITIONAL INFORMATION: 2of4brif.txt is a downloaded list of words provided for the project to be used for the search for palindromes.
Here's the code in load_dictionary:
If the input file is just a list of words (one per line) then all you need is:
pali_list = []
def normalise(s: str) -> str:
return s.strip().lower()
def ispalindrome(s: str) -> bool:
return len(s) > 1 and s == s[::-1]
with open("2of4brif.txt") as words:
for word in map(normalise, words):
if ispalindrome(word):
pali_list.append(word)
print(f"\nNumber of palindromes found = {len(pali_list)}\n")
print(*pali_list, sep="\n")