第八章 2

习题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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值