-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
33 lines (26 loc) · 1.12 KB
/
main.py
File metadata and controls
33 lines (26 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def main():
# Path to the book file
path_to_file = "books/frankenstein.txt"
# Open and read the file
with open(path_to_file) as f:
file_contents = f.read()
# Print the contents to the console
print(file_contents)
words = file_contents.split()
# Create a dictionary of String -> Integer and populate it with each character and the number of times it appears in the text after converting the text to lowercase
character_count = {}
for character in file_contents.lower():
if character.isalpha():
if character in character_count:
character_count[character] += 1
else:
character_count[character] = 1
# Print the character count using the following format: The 'x' character was found n times
print(f"--- Begin report of {path_to_file} ---")
print(f"{len(words)} words found in the document\n")
for character, count in character_count.items():
print(f"The '{character}' character was found {count} times")
print(f"--- End report ---")
# Execute the main function
if __name__ == "__main__":
main()