This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
/
chapter7.py
90 lines (82 loc) · 2.38 KB
/
chapter7.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
"""
P2PU Python 101- Chapter 7 Exercises
"""
import sys
def exercise7_1():
print '\nExercise 7.1'
while True:
file = raw_input('Enter a filename: ')
try:
f = open(file, 'rU')
break
except:
print 'enter a valid filename/filepath\n'
continue
for line in f:
print line.upper()
exercise7_2()
def exercise7_2():
print '\nExercise7.2'
numList = []
while True:
file = raw_input('Enter a filename: ')
try:
f = open(file, 'rU')
break
except:
print 'enter valid filename/path\n'
continue
for line in f:
words = line.split(' ')
if len(words[0]) == 0: continue
if words[0] != 'X-DSPAM-Confidence:': continue
try:
numList.append(float(words[1]))
except:
print 'didn\'t work\n'
print 'Average spam confidence: ',sum(numList)/len(numList),'\n'
exercise7_3()
def exercise7_3():
print '\nExercise7.3'
numList = []
while True:
file = raw_input('Enter a filename: ')
if file == 'na na boo boo':
print '''
"...The name of the song is called 'Haddocks' Eyes'!"
"Oh, that's the name of the song, is it?" Alice said, trying to
feel interested.
"No, you don't understand," the Knight said, looking a little
vexed. "That's what the name is called. The name really is, 'The Aged
Aged Man.'"
"Then I ought to have said "That's what the song is called'?"
Alice corrected herself.
"No, you oughtn't: that's quite another thing! The song is
called 'Ways and Means': but that's only what it is called you know!"
"Well, what is the song then?" said Alice, who was by this
time completely bewildered.
"I was coming to that," the Knight said. "The song really is
"A-sitting on a Gate": and the tune's my own invention."
-- Lewis Carroll, "Through the Looking Glass"
'''
sys.exit(1)
try:
f = open(file, 'rU')
break
except:
print 'enter valid filename/path'
continue
for line in f:
words = line.split(' ')
if len(words[0]) == 0: continue
if words[0] != 'X-DSPAM-Confidence:': continue
try:
numList.append(float(words[1]))
except:
print 'didn\'t work'
print 'Average spam confidence: ',sum(numList)/len(numList),'\n'
def main():
exercise7_1()
if __name__ == '__main__':
main()