习题8.5 编写一个程序,读取邮箱数据,当遇到一个以From开头的文本行,使用split函数将该行分解成单词,我们需要抽取From开头的行中第二个单词,即发信人。解析以From开头的行,打印出每行中的第二个单词,还可以统计发信人数,在结尾输出总数。
fname = raw_input('Please enter the file name:')
try:
fhand = open(fname)
except:
print 'Please enter correct file name!'
count = 0
for line in fhand:
if not line.startswith('From'): continue
count = count + 1
words = line.split()
print words[1]
print 'There are %d lines in the file with From as the first word.', count
习题8.6 编写一个程序,使用列表储存用户输入的数据,在循环结束后,利用max()和min()函数分别计算出最大值和最小值
numlist =[]
while True:
num = raw_input('Enter a number:')
if num == 'done':
break
numlist.append(num)
maxi = float(max(numlist))
mini = float(min(numlist))
print 'Maximum: ',maxi
print 'Minimum: ',mini