Article Directory
1、BUG
1.1 The origin and classification of bugs
The origin of the bug: the evolution of the world's first universal computer-Mark II (Mark Ⅱ)
Debug: Eliminate errors
1.2 Common types
#语法错误SyntaxError (粗心导致)
#1
age=input('你的年龄')
if int(age)>=18: #输入的是str类型,因此要转为int类型
print('成年')
#2 要给i赋值,并且能让循环停下来
i=1
while i<10:
print(i)
i += 1
#3
'''
比较用==,赋值用=
缩进错误
末尾的冒号
不能英文符号和数字符号写在一起
没有定义变量,比如while的循环条件
字符串拼接的时候,字符串不能和数字拼在一起
'''
#知识不熟练导致的错误
#1.索引越界IndexError
lst=[11,22,33,44]
print(lst[3]) #不能写4,否则越界
#append()方法不熟练
lst=[]
lst.append('A')
lst.append('B') #每次只能添加一个元素
'''
思路不清导致的问题
解放方式
(1)用print()
(2)用#注释部分代码
'''
'''
被动掉坑:程序代码逻辑没有错,只是因为用户错误操作或者一些‘例外情况’而导致的程序崩溃
'''
#异常处理机制
try:
a = int(input('第一个整数'))
b = int(input('第二个整数'))
result = a / b
print('结果为:', result)
except ZeroDivisionError:
print('对不起,除数不能为0')
except ValueError:
print('只能输入数字串')
print('程序结束')
2. Try-except-else structure & try-except-finally structure
#异常处理机制
try:
a = int(input('第一个整数'))
b = int(input('第二个整数'))
result = a / b
except BaseException as e:
print('出错了',e)
else:
print('结果为:', result)
print('程序结束')
try:
a = int(input('第一个整数'))
b = int(input('第二个整数'))
result = a / b
except BaseException as e:
print('出错了',e)
else:
print('结果为:', result)
finally:
print('谢谢您的使用')
print('程序结束')
3. Common exception types
'''
(1)数学运算异常 ZeroDivisionError
(2)序列中没有此索引 IndexError
(3)映射中没有这个键 KeyError
(4)未声明/初始化对象 NameError
(5)语法错误 SyntaxError
(6)传入无效的参数 ValueError
'''
4. 8traceback module
#打印日常
import traceback
try:
print('-----------')
print(1/0)
except:
traceback.print_exc()
5. Debugging of Pycharm program
Breakpoint
Debug
press this