歡迎您光臨本站 註冊首頁

python中如何進行連乘計算

←手機掃碼閱讀     qp18502452 @ 2020-06-06 , reply:0

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 連乘 遞歸 參數可以是多個可迭代對象

  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))

                       

   


[qp18502452 ] python中如何進行連乘計算已經有1168次圍觀

http://coctec.com/docs/python/shhow-post-237234.html