From 8a69d379b95f1a8f11767622c212f917078fc435 Mon Sep 17 00:00:00 2001 From: lolabird Date: Sun, 19 Nov 2023 16:24:06 -0700 Subject: [PATCH] Count words in file --- thinkcspy/Chapter-12-Exercises/Problem3.py | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/thinkcspy/Chapter-12-Exercises/Problem3.py b/thinkcspy/Chapter-12-Exercises/Problem3.py index 82340dc..41b5d2e 100644 --- a/thinkcspy/Chapter-12-Exercises/Problem3.py +++ b/thinkcspy/Chapter-12-Exercises/Problem3.py @@ -1 +1,24 @@ -open(alice_in_wonderland.txt, 'r') \ No newline at end of file +aliceBook = open("thinkcspy/Chapter-12-Exercises/alice_in_wonderland.txt", "r") +aliceDict = {} + +for line in aliceBook: + words = line.split() + + for word in words: + word = word.lower() + newWord = "" + + for char in word: + if char.isalpha(): + newWord += char + + word = newWord + + if word not in aliceDict: + aliceDict[word] = 1 + else: + aliceDict[word] += 1 + +print(aliceDict) + +aliceBook.close()