鱼C论坛

 找回密码
 立即注册
查看: 1385|回复: 7

[已解决]python日志

[复制链接]
发表于 2022-5-4 21:31:37 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2022-5-4 21:39 编辑

使用 logging.info 才是写入日志,你那个是初始化日志,

而且 logging 本来就是你导入的库,不需要在 init_log 中返回的
  1. import logging
  2. import os
  3. import random


  4. def init_log(path):
  5.     if os.path.exists(path):
  6.         mode = 'a'
  7.     else:
  8.         mode = 'w'
  9.     logging.basicConfig(
  10.         level=logging.INFO,
  11.         format='%(asctime)s %(filename)s %(lineno)d %(message)s',
  12.         filename=path,
  13.         filemode=mode
  14.     )

  15. def guess_number():
  16.     print('********************欢迎进入数字踩踩踩游戏********************')
  17.     init_log('record.txt')                         # 初始化一次 logging 就可以了
  18.     start = input('数字区间起始值:')
  19.     end = input('数字区间终止值:')
  20.     if not start.isdigit() or not end.isdigit():
  21.         print('您输入的区间数字为非数字字符!!请重新启动程序。')
  22.         exit()
  23.     else:
  24.         start = int(start)
  25.         end = int(end)
  26.         count = 0
  27.         if start == end:
  28.             print('您输入的区间数字相同!!请重新启动程序。')
  29.             exit()
  30.         elif start > end:
  31.             print('您输入的区间数字大小有误!!请重新启动程序。')
  32.             exit()

  33.         else:
  34.             print('所产生的的随机数字区间为:[{}, {}]'.format(start, end))
  35.             random_number = random.randint(start, end)

  36.             while True:
  37.                 count += 1
  38.                 number = int(input('请继续输入您猜测的数字:'))
  39.                 if number not in range(start,end+1):
  40.                     print('对不起您输入的数字未在指定区间内!!!')
  41.                     continue
  42.                 elif number > random_number:
  43.                     print('*********')
  44.                     print('Higher than the anwser')
  45.                     logging.info('Higher than the anwser')     # 使用 logging.info
  46.                     continue
  47.                 elif number < random_number:
  48.                     print('*********')
  49.                     print('Lower than the anwser')
  50.                     logging.info('Lower than the anwser')      # 使用 logging.info
  51.                     continue
  52.                 elif number == random_number:
  53.                     print('*********')
  54.                     print('恭喜你!只用了{}次就赢得了游戏'.format(count))
  55.                     logging.info('恭喜你!只用了{}次就赢得了游戏'.format(count))  # 使用 logging.info
  56.                     break

  57. if __name__ == '__main__':
  58.     guess_number()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-4 21:41:17 | 显示全部楼层
本帖最后由 isdkz 于 2022-5-4 21:42 编辑
哈岁NB 发表于 2022-5-4 21:38
那请问一下,我该怎样修改才能把判断的相应信息写入日志呢


init_log 只需要在主函数开头调用一次来初始化 logging,

后面你要写入日志就调用 logging.info("这里写你要写入的日志") 就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-19 03:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表