调试

笑话:“编码占了编程工作量的90%,调试占了另外 90%。

抛出异常

#raise 语句引发的异常通;常用try、except(else、finally)异常处理结构来捕获并进行处理

try:
    a = input("输入一个数:")
    if(not a.isdigit()):
        raise ValueError("a 必须是数字")
except ValueError as e:
    print("引发异常:",repr(e))

反向跟踪的字符串

traceback.format_exc()		#显示反向跟踪

断言

assert 表达式 [, 参数]
	 #1. 当表达式为真时,程序继续往下执行; 
	 #2. 当表达式为假时,抛出AssertionError错误,并将  参数  输出
	 
python -O 文件名		#禁用断言

日志

import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.debug('Start of program')
def factorial(n):
    logging.debug('Start of factorial(%s%%)' % (n))
    total = 1
    for i in range(1, n + 1):
        total *= i
        logging.debug('i is ' + str(i) + ', total is ' + str(total))
    logging.debug('End of factorial(%s%%)' % (n))
    return total
print(factorial(5))
logging.debug('End of program')

# logging.debug()	打印内容:包含字符串、时间戳、单词DEBUG

日志级别

DEBUG 		$logging.debug() 		最低级别。通常只有在诊断问题时,你才会关心这些消息
INFO 		$logging.info() 		用于记录程序中一般事件的信息,或确认一切工作正常
WARNING 	$logging.warning() 		用于表示可能的问题,它不会阻止程序的工作,但将来可能会
ERROR 		$logging.error() 		用于记录错误,它导致程序做某事失败
CRITICAL 	$logging.critical() 	最高级别。用于表示致命的错误,它导致或将要导致程序完全停止工作

#禁止日志
logging.disable(logging.CRITICAL)      

#日志记录到文件
logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')		
import logging
>>> logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
>>> logging.debug('Some debugging details.')
2015-05-18 19:04:26,901 - DEBUG - Some debugging details.
>>> logging.info('The logging module is working.')
2015-05-18 19:04:35,569 - INFO - The logging module is working.
>>> logging.warning('An error message is about to be logged.')
2015-05-18 19:04:56,843 - WARNING - An error message is about to be logged.
>>> logging.error('An error has occurred.')
2015-05-18 19:05:07,737 - ERROR - An error has occurred.
>>> logging.critical('The program is unable to recover!')
2015-05-18 19:05:45,794 - CRITICAL - The program is unable to recover!

项目:调试硬币抛掷

调试前
import random
guess = ''
while guess not in ('heads', 'tails'):
    print('Guess the coin toss! Enter heads or tails:')
    guess = input()
toss = random.randint(0, 1)
if toss == guess:
    print('You got it!')
else:
    print('Nope! Guess again!')
    guesss = input()
    if toss == guess:
        print('You got it!')
    else:
        print('Nope. You are really bad at this game.')
调试后
import random
guess = ''
while guess not in ('heads', 'tails'):
    print('Guess the coin toss! Enter heads or tails:')
    guess = input()
toss = random.randint(0, 1)
if toss == 0:
    toss == 'tails'
else:
    toss == 'heads'
if toss == guess:
    print('You got it!')
else:
    print('Nope! Guess again!')
    guesss = input()
    if toss == guess:
        print('You got it!')
    else:
        print('Nope. You are really bad at this game.')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值