【程序18】求s=a+aa+aaa+aaaa+aa...a的值

求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。
知识点:在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里
用的话要 先引入
from functools import reduce

Tn = 0
Sn = []
n = int(input('n = '))
a = int(input('a = '))
for count in range(n):
    Tn = Tn + a
    a = a * 10
    Sn.append(Tn)
from functools import reduce
Sn = reduce(lambda x,y : x + y,Sn)
print('Sn = %d' % Sn)

运行结果:

n = 5
a = 2
Sn = 24690

Process finished with exit code 0
原文地址:https://www.cnblogs.com/fanren224/p/8457266.html