1 # coding=utf-8 2 # Created by danding at 2019/10/31 3 4 from math import * 5 6 def jiechenghe(num): 7 list = [] 8 k = 1 9 for i in range(1,num+1): 10 k = k*i 11 list.append(k) 12 sum = 0 13 for i in list: 14 sum += i 15 16 print('前{}项的阶乘和是:{}'.format(num,sum)) 17 18 while 1: 19 try: 20 num = input('提示:输入"q"退出程序。 请输入前几项数字的阶乘和:') 21 if num.lower()=='q': 22 break 23 num = int(num) 24 except: 25 print('请输入数!') 26 continue 27 jiechenghe(num)