一、调试的基本概念
二、
1、__name__=="__main__"
这个函数是用来保证py文件,既可以独立使用,又可以被调用。
如果没有__name__=="__main__"
只要载入就会调用函数,所以使用起来会带来麻烦
2、pdb调试
1 _DEBUG = True #方便打开和关闭调试
2 def debug_bmi(height, weight, gender):
3 if _DEBUG == True:
4 import pdb #载入pdb库
5 pdb.set_trace() #设置断点
6 if gender != 'male' and gender !='female':
7 print("input error")
8 elif gender == 'male':
9 standard_weight = (height - 100)*0.9
10 else:
11 standard_weight = (height - 100)*0.9 - 2.5
12 if weight <= (standard_weight*0.9):
13 print ("You BMI is -1")
14 elif weight <(standard_weight*1.1):
15 print ("You BMI is 0")
16 elif weight <(standard_weight*1.2):
17 print ("You BMI is 1")
18 elif weight <(standard_weight*1.3):
19 print ("You BMI is 2")
20 elif weight <(standard_weight*1.5):
21 print ("You BMI is 3")
22 else:
23 print ("You BMI is 4")
24
25 if __name__ == "__main__":
26 debug_bmi(160,60,'male')
(1)查看代码上下文:l(小写的L)
(2)监视变量: p 变量名
(3)单步执行:n
(4)加入断点:b 行号