Latest web development tutorials

Python includes exercises 18

Python 100 Li Python 100 Li

Title: Demand s = a + aa + aaa + aaaa + aa ... a value, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (In this case the sum total of the number 5), a few numbers together with a keyboard control.

Program Analysis: The key is to calculate the value of each item.

Source Code:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

Tn = 0
Sn = []
n = int(raw_input('n = :\n'))
a = int(raw_input('a = :\n'))
for count in range(n):
    Tn = Tn + a
    a = a * 10
    Sn.append(Tn)
    print Tn

Sn = reduce(lambda x,y : x + y,Sn)
print Sn

The above example output is:

n = :
2
a = :
4
4
44
48

Python 100 Li Python 100 Li