猫哥教你写爬虫 019--debug-做业

期末考试结束了,小加在教务系统查到了本身的几门必修课分数,

他想经过python计算本身的平均分。

因而写了下面的代码,但是老是得不到结果,请帮纠正bug并跑通程序。

scores = {'语文':89, '数学':95, '英语':80}
sum_score = 0
def get_average(scores):
    for subject, score in scores.items():
        sum_score += score
        print('如今的总分是%d'%sum_score)
    ave_score = sum_score/len(scores)
    print('平均分是%d'%ave_score)
get_average(scores)
复制代码

改正后的代码

scores = {'语文':89, '数学':95, '英语':80}
def get_average(scores):
    sum_score = 0 # 应该是局部变量
    for subject, score in scores.items():
        sum_score += score
        print('如今的总分是%d'%sum_score)
    ave_score = sum_score/len(scores)
    print('平均分是%d'%ave_score)
get_average(scores)
复制代码

小强认识了一个新朋友叫旺财,他想让你给他取个外号,但他很不喜欢别人叫他小狗和汪汪,

因而写了一个程序让本身避免叫他这两个外号中的一个,但是不知为何叫他小狗程序也没有警告。

not_bad_word = True
while not_bad_word:
    x = input('请给旺财取个外号:')
    if x == '小狗' and x =='汪汪':
        not_bad_word = False
        print('我生气了,不想理你了!')
print('对不起,之后我不会这么叫你了')
复制代码

改正后的代码

not_bad_word = True
while not_bad_word:
    x = input('请给旺财取个外号:')
    if x == '小狗' or x =='汪汪': # 应该使用or
        not_bad_word = False
        print('我生气了,不想理你了!')
print('对不起,之后我不会这么叫你了')
复制代码

小明想用python写个程序,看看本身的存款每月涨了多少倍。

但是发现程序报错,你能帮他找出错误,使程序从新运行吗?

deposit = [100,300,900,2000,5000,0,2000]
for i in range(1, len(deposit)):
    times = deposit[i]/deposit[i-1]
    print('你的存款涨了%f倍'%times)
复制代码

等于0的时候会报错...

改正后的代码...

deposit = [100,300,900,2000,5000,0,2000,4500]
for i in range(1, len(deposit)):
    if deposit[i-1] == 0:  # 判断除数等于0时,特殊处理。
        print('你上次存款为 0 哦!')
    else:
        times = deposit[i]/deposit[i-1]
        print('你的存款涨了%f倍'%times)
复制代码

练习目标:

经过这个练习,咱们会用代码作出一个贴心的除法计算器:

只要输入有误,就会给出相应的报错信息。

练习要求:

这个除法计算器须要包含的报错信息有:

输入了非数值(即不属于整数和浮点数)、除数为零以及变量不存在。

为了让代码能够给出相应的报错信息,咱们能够运用课堂中谈到的try...except语句。

1558661105223
1558661124212
1558661138571
1558661176415

print('欢迎使用除法计算器')
while True:
    first = input('请你输入被除数: ')
    second = input('请你输入除数: ')
    try:
        result = float(first)/float(second)
        print("{} / {} = {}".format(first,second,result))
        break
    except ZeroDivisionError:
        print('0不能作除数')
    except ValueError:
        print('除数和被除数应该为整数或者浮点数')
复制代码

快速跳转:

猫哥教你写爬虫 000--开篇.md
猫哥教你写爬虫 001--print()函数和变量.md
猫哥教你写爬虫 002--做业-打印皮卡丘.md
猫哥教你写爬虫 003--数据类型转换.md
猫哥教你写爬虫 004--数据类型转换-小练习.md
猫哥教你写爬虫 005--数据类型转换-小做业.md
猫哥教你写爬虫 006--条件判断和条件嵌套.md
猫哥教你写爬虫 007--条件判断和条件嵌套-小做业.md
猫哥教你写爬虫 008--input()函数.md
猫哥教你写爬虫 009--input()函数-人工智能小爱同窗.md
猫哥教你写爬虫 010--列表,字典,循环.md
猫哥教你写爬虫 011--列表,字典,循环-小做业.md
猫哥教你写爬虫 012--布尔值和四种语句.md
猫哥教你写爬虫 013--布尔值和四种语句-小做业.md
猫哥教你写爬虫 014--pk小游戏.md
猫哥教你写爬虫 015--pk小游戏(全新改版).md
猫哥教你写爬虫 016--函数.md
猫哥教你写爬虫 017--函数-小做业.md
猫哥教你写爬虫 018--debug.md
猫哥教你写爬虫 019--debug-做业.md
猫哥教你写爬虫 020--类与对象(上).md
猫哥教你写爬虫 021--类与对象(上)-做业.md
猫哥教你写爬虫 022--类与对象(下).md
猫哥教你写爬虫 023--类与对象(下)-做业.md
猫哥教你写爬虫 024--编码&&解码.md
猫哥教你写爬虫 025--编码&&解码-小做业.md
猫哥教你写爬虫 026--模块.md
猫哥教你写爬虫 027--模块介绍.md
猫哥教你写爬虫 028--模块介绍-小做业-广告牌.md
猫哥教你写爬虫 029--爬虫初探-requests.md
猫哥教你写爬虫 030--爬虫初探-requests-做业.md
猫哥教你写爬虫 031--爬虫基础-html.md
猫哥教你写爬虫 032--爬虫初体验-BeautifulSoup.md
猫哥教你写爬虫 033--爬虫初体验-BeautifulSoup-做业.md
猫哥教你写爬虫 034--爬虫-BeautifulSoup实践.md
猫哥教你写爬虫 035--爬虫-BeautifulSoup实践-做业-电影top250.md
猫哥教你写爬虫 036--爬虫-BeautifulSoup实践-做业-电影top250-做业解析.md
猫哥教你写爬虫 037--爬虫-宝宝要听歌.md
猫哥教你写爬虫 038--带参数请求.md
猫哥教你写爬虫 039--存储数据.md
猫哥教你写爬虫 040--存储数据-做业.md
猫哥教你写爬虫 041--模拟登陆-cookie.md
猫哥教你写爬虫 042--session的用法.md
猫哥教你写爬虫 043--模拟浏览器.md
猫哥教你写爬虫 044--模拟浏览器-做业.md
猫哥教你写爬虫 045--协程.md
猫哥教你写爬虫 046--协程-实践-吃什么不会胖.md
猫哥教你写爬虫 047--scrapy框架.md
猫哥教你写爬虫 048--爬虫和反爬虫.md
猫哥教你写爬虫 049--完结撒花.mdhtml

相关文章
相关标签/搜索