logging模块的使用编写硬币游戏

import random,logging
logging.basicConfig(filename='E:\\python-project\\auto_project_10\\10.8.txt',level=logging.DEBUG,format='%(asctime)s-%(levelname)s:%(message)s')
#filename 写入文件的绝对路径,level代表级别有DEBUG,WARNING,INFO等
#format代表格式 asctime操作时间  levelname信息级别  message个人附加信息
logging.debug('start new 10.8.py project')

def main():
    logging.debug('start of main project')
    guss_coin()


def guss_coin():
    guss=7
    logging.debug('start guss_coin coin is %s'%(guss))
    while guss!='':
        guss=random.randint(0,1)
        logging.debug('start guss_coin coin is %s' %(guss))
        print('请猜一下正反(正:1,反:0)')
        your_choose=input()
        your_choose=int(your_choose)
        if your_choose==1 or your_choose==0:
            logging.debug('your_choose is wrong,choose is %s' %(your_choose))
            #向路径下插入的内容为your_choose is wrong,choose is <your_choose>这个变量值
            if your_choose==guss:
                print('猜对了')
                logging.debug('your_choose is %s' % (your_choose))
                continue
            else:
                print('猜错了')
                continue
            print('请输入0或1\n')
            continue
            logging.debug('猜对一次')
        elif your_choose==8:
            logging.debug('跳出程序\n')
            guss=''
            continue
        else:
            logging.debug('your choose is %s'%(your_choose)+'real choose is %s'%(guss))
            print('请输入0或1')
            logging.debug('输入错误')
            continue

if __name__=='__main__':
    main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
logging模块Python用于记录日志的标准库。下面是一个使用logging模块的基本场景使用实例\[1\]: ```python import logging # 配置日志输出的格式 logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) # 创建一个logger对象 logger = logging.getLogger(__name__) # 输出不同级别的日志 logger.debug('This is a debug message') logger.info('This is an info message') logger.warning('This is a warning message') logger.error('This is an error message') logger.critical('This is a critical message') ``` 在上面的例子,我们首先通过`basicConfig`函数配置了日志输出的格式和级别。然后,我们创建了一个名为`__name__`的logger对象,这样做的好处是可以在日志明确显示消息来自哪个模块\[2\]。接下来,我们使用不同级别的方法来输出日志信息。 除了上述基本用法,还可以通过配置文件或使用`logging.config`模块来配置日志\[3\]。通过配置文件可以更灵活地配置不同模块的日志输出。在实际开发,可以先编写好一个配置文件,然后在主函数通过`fileConfig`加载配置文件。然后,在不同的模块可以使用根logger的子logger来进行日志记录,而不需要反复定义和配置各个模块的logger。 希望以上内容对您有帮助。如果需要更详细和灵活的使用方法,请参考官方帮助文档:https://docs.python.org/zh-cn/3/howto/logging.html。 #### 引用[.reference_title] - *1* [[ Python入门教程 ] Python日志记录模块logging使用实例](https://blog.csdn.net/weixin_32687283/article/details/113652063)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [logging模块详细使用](https://blog.csdn.net/lucylily11/article/details/116050564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值