猫史档案馆


【Python作品分享】练习3-工程包-2

用户:灵巧的金冠泥8a-4灵巧的金冠泥8a-4查看:0 回复:0 评论:0 创建时间:2022-07-17T15:21:30


【作品展示】

center_image

 

【作品介绍】

hahaha

 

【作品源代码】

'''
练习3-工程包
t: 包含病毒的文件内容
b: 病毒名称
bd: 病毒种类列表
sniped: 从文件bd_butler.txt中消灭掉的病毒名称及数量信息,存储格式为(b,num)
'''

def snipe_bd(t, b):
    num = t.count(b)
    if num > 0:
        t = t.replace(b, '')  # 把butler.txt文件内容中的病毒名称删除掉
    else:
        raise ValueError('num值为0')  # 如果统计某种病毒数量为0,即sum==0,抛出ValueError异常
    return num, t

bd = ['SARS', 'MERS', 'COVID-19']
sniped = []
try:
    f = open('bd_butler.txt','r')
except IOError:
    print('没有找到文件!')
else:
    print('病毒狙击开始!')
    t = f.read()
    f.close()
    for b in bd:
        try:
            snipe = snipe_bd(t,b)
            num = snipe[0]
            sniped.append((b,num))
        except ValueError:
            print('未发现病毒:',b)
    print("病毒狙击完成,狙击详情为:",sniped)

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页