**
第十章 测试硬币抛掷
**
将调试硬币抛掷的代码修改了一下,加入了assert测试语句,同时还加入了关键节点的日志信息。
#!python
#测试硬币抛掷.py
import random,logging#导入了随机数及日志模块
logging.basicConfig(filename='调试硬币抛掷.txt',level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')#设置日志模块基本框架,将日志保存进txt文本
guess=''
while guess not in ('heads','tails'):
guess=input("Enter your guess,heads or tails:")
logging.debug('The guess is '+str(guess))
toss=random.randint(0,1)#0 is tails,1 is heads
logging.debug('The toss is '+str(toss))
if toss==0:
toss='tails'
elif toss==1:
toss='heads'
logging.debug('The toss is %s'%toss)
assert toss in ('heads','tails'),'The random not in this.'#assert测试,如果toss不是头或字,程序报错并提示后段信息
if toss==guess:
print('You got it!')
logging.debug('You got it!')
else:
guesss=input('Enter your guess again:')
logging.debug('The guesss is %s'%guesss)
if guesss == toss:
print('Yout got it!')
logging.debug('Yout got it!')
else:
print('Nope,you are really bad at this game.')
logging.debug('Nope,you are really bad at this game.')```