-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.py
33 lines (24 loc) · 1011 Bytes
/
counter.py
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
""" Counts letters in user input.
btw I wanted to use dictionaries, not set() and list"""
print("This will take whatever you write and count all the letters in it.")
user_input = input("Enter something: ")
character_dictionary = {}
char_lenght = {}
sorted_character_dictionary = {}
def sorter():
""" Sorts user input in dictionaries by letters"""
for i in user_input.lower():
# Loops through user_input checking for letters, discarding all the other characters
if str(i).isalpha():
if f"{i}" in character_dictionary:
character_dictionary[i].append(i)
else:
character_dictionary[i] = [i]
def sort_and_count():
# Sorts character dictionary and counts letters
sorted_character_dictionary = dict(sorted(character_dictionary.items()))
for key in sorted_character_dictionary:
print("Letter", key.upper(), "was used:", len(
sorted_character_dictionary[key]), "times.")
sorter()
sort_and_count()