神剑山庄资源网 Design By www.hcban.com

1、Python中连乘的代码:

sum = 1;
n = int(input("Please input number n:"))
for i in range(1,n+1):
sum = sum*i;
if i<n:
print(i,end='')
print("*",end = '')
print(i,"=",sum)

2、运行结果

python中如何进行连乘计算

实例扩展:

python 连乘 递归 参数可以是多个可迭代对象

from functools import reduce

a = (1, 2, 3, ['1','1'], [1, [2, [3, [4]]]])

def args_all_to_list(*args):
  try:
    data=list(*args)
    return data
  except:
    data=list(args)
    return data

def data_list(a):
  data = []
  f = 0
  for x in a:
    if type(x) is int:
      data.append(x)
      f += 1
    elif type(x) is str:
      data.append(int(x))
    elif type(x) is list or tuple:
      data += list(x)
    if f == len(a):
      return data
  return data_list(data)


def chen(*args):
  return reduce(lambda x, y: x * y, data_list(args_all_to_list(*args)))


print(chen(1,2))
print(chen(1,2,[1]))
print(chen(a))
标签:
python,连乘计算

神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com

评论“python中如何进行连乘计算”

暂无python中如何进行连乘计算的评论...