-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex10.3.py
More file actions
36 lines (30 loc) · 840 Bytes
/
ex10.3.py
File metadata and controls
36 lines (30 loc) · 840 Bytes
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
34
35
36
import string
fnand = input('Enter file: ')
try:
lines = open(fnand)
except:
print('File cannot be opened')
exit()
counts = dict()
totals = 0
average = 0
for line in lines:
#line = line.translate(line.maketrans('', '', string.punctuation + string.digits))
line = line.rstrip()
#line = line.lower()
line = line.split()
for word in line:
for letter in word:
totals += 1
if letter not in counts:
counts[letter] = 1
else:
counts[letter] += 1
letters = list()
for key, val in list(counts.items()):
letters.append((key, val))
letters.sort()
for key, val in letters:
average = round(val / totals * 100, 2)
print(key, 'shows up', average, 'percent or', val, 'times in', fnand)
print('Iterated though', totals, 'letters.')