Cat brother teach you to write reptile 018 - debug

History of bug

The first story only bug

The computer program of mother Grace Herbert (Grace Hopper).

Time back in 1947, when she was a monster programmed for the next drawing this ...

The first version of universal evolution of computer - Mark No. (Mark II) 2

bugOn the Incarnation synonymous with computer field program fault

As the saying goes, coding five minutes, debug two hours

This code will complain it?

a = input('请输入密码:')
if a == '123456'
    print('通过')
复制代码

There is a piece of code is wrong ...

for x in range(10):
     print(x)  
for x in range(10):
    print(x)  
复制代码

Look at this section ...

n = 0
while n<3:
    username = input("请输入用户名:")
    password = input("请输入密码:")
    if username == 'abc' and password == '123':
        print("登录成功")
        break
    else:
        n=n+1
        print("输入有误")
else:
    print("你输错了三次,登录失败")
复制代码

Come together stubble ...

From the list of found Sunday

week = ['星期一','星期二','星期三','星期四','星期五','星期六','星期日']
sunday = week[7]
print(sunday)
复制代码

Create an empty list, is inserted inside the value ...

a = []
a = append ('A','B','C')
print(a)
复制代码

When you find that knowledge can not remember or can not be determined when necessary and timely review or search the Internet.

Do not force yourself to write code is not sure, this situation often prone to error.

Unclear thinking means that when we solve more complex problems, because we think clearly enough to the details and means of achieving,

Either step leading to wrong, wrong step; either, although there is no error, but the program does not achieve the results we want.

Before leaving the job ...

This code is the problem?

movie = {
    '妖猫传':['黄轩','染谷将太'],
    '无问西东':['章子怡','王力宏','祖峰'],
    '超时空同居':['雷佳音','佟丽娅']
}
name=input('你查询的演员是?')
for i in movie:
    actors=[i]
    if name in actors:
        print(name+'出演了'+i)
复制代码

Guess coins ...

import random
guess = ''
while guess not in ['正面','反面']:
    print('------猜硬币游戏------')
    print('猜一猜硬币是正面还是反面?')
    guess = input('请输入“正面”或“反面”:')
# 随机抛硬币,0代表正面,1代表反面
toss = random.randint(0,1)
if toss == guess:
    print('猜对了!你真棒')
else:
    print('没猜对,你还有一次机会。')
    guess = input('再输一次“正面”或“反面”:')
    if toss == guess:
        print('你终于猜对了!')
    else:
        print('大失败!')
复制代码

age = int(input('你今年几岁了?'))
if age < 18:
    print('不可以喝酒噢')
复制代码

If not a number ...

So the solution is ...

try:
    age = int(input('你今年几岁了?'))
    if age < 18:
        print('不可以喝酒噢')
except ValueError:
    print('你喝多了吧...')
复制代码

This code has no problem?

num = [1,2,0,3]
for x in num:
    print (6/x)
复制代码

So the solution is ....

num = [1,2,0,3]
for x in num:
    try:
        print (6/x)
    except ZeroDivisionError:
        print('0不能做除数')
复制代码

All abnormal summary ...

The exception name description
BaseException Base class for all exceptions
SystemExit Interpreter exit request
KeyboardInterrupt User Interrupt Executing (usually enter ^ C)
Exception General Error base class
StopIteration Iterator no more value
GeneratorExit To notify the exit exception generator (generator) occurs
StandardError All standard exceptions built base class
ArithmeticError All numerical errors base class
FloatingPointError Floating-point calculation error
OverflowError Exceeds the maximum limit value calculation
ZeroDivisionError In addition to (or modulus) of zero (all data types)
AssertionError Assertion failure
AttributeError Object does not have this property
EOFError No built-in input, to reach the EOF marker
EnvironmentError OS Error base class
IOError Input / output operations to fail
OSError Operating system error
WindowsError System call fails
ImportError Import module / object failed
LookupError Invalid class data base query
IndexError Without this sequence index (index)
KeyError Without this key mapping
MemoryError Memory overflow error (for Python interpreter is not fatal)
NameError Undeclared / initialize objects (no attributes)
UnboundLocalError Local access uninitialized variables
ReferenceError Weak reference object (Weak reference) have been trying to access a garbage collection of
RuntimeError General runtime error
NotImplementedError The method has not been implemented
SyntaxError Python syntax error
IndentationError Indentation errors
TabError Tab and space mix
SystemError General interpreter system error
TypeError Invalid type of operation
ValueError Invalid parameter passed
UnicodeError Unicode related errors
UnicodeDecodeError Error when decoding of Unicode
UnicodeEncodeError Unicode encoding error
UnicodeTranslateError Unicode conversion error
Warning Warning base class
DeprecationWarning Warning about deprecated features
FutureWarning Warning about the future structure of the semantics have changed
overflow Warning The old warning about automatically promoted to a long integer (long) of
PendingDeprecationWarning Will be warned about the characteristics of the waste
RuntimeWarning Suspicious behavior (runtime behavior) run-time warning
SyntaxWarning Suspicious Warning grammar
UserWarning User code generated warning

Final exam is over, a small increase in the educational system found their score a few required courses,

He wanted to calculate their average score by python.

So wrote the following code, but not always results, please help correct the bug and run through the program.

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)
复制代码

Xiaoqiang met a new friend named Cai, he wants you to give him a nickname, but he did not like to be called his dog and bark,

于是写了一个程序让自己避免叫他这两个外号中的一个,可是不知为什么叫他小狗程序也没有警告。

not_bad_word = True
while not_bad_word:
    x = input('请给旺财取个外号:')
    if x == '小狗' and x =='汪汪':
        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)
复制代码

练习目标:

通过这个练习,我们会用代码做出一个贴心的除法计算器:

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

练习要求:

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

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

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

快速跳转:

猫哥教你写爬虫 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--完结撒花.md

转载于:https://juejin.im/post/5cfc4ad86fb9a07ebd48c902

Guess you like

Origin blog.csdn.net/weixin_33743248/article/details/91459398