Skip to content

Commit 733eb06

Browse files
committed
#files
1 parent 7524cfe commit 733eb06

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

ActivitySet#1/problem#08.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1-
# Files
1+
# # Files
22

3-
filename = "dataset/mbox-short.txt"
3+
# # filename = "dataset/mbox-short.txt"
4+
5+
# 7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:
6+
# X-DSPAM-Confidence: 0.8475
7+
# Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution.
8+
9+
10+
# Use the file name mbox-short.txt as the file name
11+
fname = input("Enter file name: ")
12+
13+
try:
14+
fhand = open(fname)
15+
except:
16+
print("the file doesn't exist")
17+
quit()
18+
19+
count = 0
20+
average = 0
21+
22+
for line in fhand:
23+
if not line.startswith("X-DSPAM-Confidence:"):
24+
continue
25+
else:
26+
position = line.find(":")
27+
average+=float(line[position+1:])
28+
count+=1
29+
average = average/count
30+
print("Average spam confidence:",average)
31+

0 commit comments

Comments
 (0)