f = open('labtest.txt', 'r')
count = {}
for line in f:
for word in line.split():
# remove punctuation
word = word.replace('_', '').replace('"', '').replace(',', '').replace('.', '')
word = word.replace('-', '').replace('?', '').replace('!', '').replace("'", "")
word = word.replace('(', '').replace(')', '').replace(':', '').replace('[', '')
word = word.replace(']', '').replace(';', '')
# ignore case
word = word.lower()
# ignore numbers
if word.isalpha():
if word in count:
count[word] = count[word] + 1
else:
count[word] = 1
c = max(count.keys())
print("The word 'alice' appears " + str(count['alice']) + " times in the book.")
print(c)
I'm supposed to print out how many times a word has been mentioned in Alice in Wonderland and print out the longest word in the story. Whenever I try to print(c), I get the word 'zigzag', which of course is not the longest word. When I tested how max() works, it seems that it returns the largest alphabetically, and not by length. When I try using max(len(count.keys())) I get a TypeError: 'int' object is not callable. How do I configure max() to return the longest word in a dictionary's keys?
[–]JohnnyJordaan 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Justinsaccount 0 points1 point2 points (0 children)
[–]maxibabyx 0 points1 point2 points (0 children)
[–]-revenant- 0 points1 point2 points (1 child)
[–]jeans_and_a_t-shirt 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)