我编写了一个代码来查找阶乘,我使用了日志记录模块。搜索该网站后,我还没有找到相关问题。这是我的代码
def factorial(n):
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s-%(levelname)s-%(message)s')
logging.debug('Start of program')
logging.debug('start of action (%s)'%(n))
total=1
for i in range(1,n+1):
#print(i)
total *=i
print(total)
logging.debug('i is '+str(i)+' total='+str(total))
logging.debug('End of factorial(%s) '%(n))
return total
def main():
print(factorial(5))
logging.debug('End of program')
if __name__=='__main__':
import logging
main()
total 的输出不是“它应该是什么”
logging.debug('i is '+str(i)+' total='+str(total))
这是我运行代码时得到的
2018-11-21 09:34:56,213-DEBUG-Start of program
2018-11-21 09:34:56,213-DEBUG-start of action (5)
1
2018-11-21 09:34:56,213-DEBUG-i is 1 total=12
2018-11-21 09:34:56,213-DEBUG-i is 2 total=2
6
2018-11-21 09:34:56,213-DEBUG-i is 3 total=6
24
2018-11-21 09:34:56,213-DEBUG-i is 4 total=24
120
2018-11-21 09:34:56,213-DEBUG-i is 5 total=120
2018-11-21 09:34:56,213-DEBUG-End of factorial(5)
120
2018-11-21 09:34:56,213-DEBUG-End of program
这个问题看起来很长,但它应该是非常简单的操作。
临摹微笑
相关分类