python编程快速上手 — 让繁琐工作自动化

**

第十章 测试硬币抛掷

**
将调试硬币抛掷的代码修改了一下,加入了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.')```

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值