Small example of python2.7 exercise (17)

17): Title: find the value of s=a+aa+aaa+aaaa+aa...a, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (there are 5 numbers added in total at this time), and the addition of several numbers is controlled by the keyboard.

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

Program source code:

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

Tn = 0
Sn = []
n = int(raw_input('n = '))
a = int(raw_input('a = '))
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 "calculated and are:", Sn

The output result of the above example is:

n = 4
a = 4
4
44
444
4444
 Calculated sum: 4936
    
#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = int(raw_input("a:"))
n = int(raw_input("n:"))
list=[]
for i in range(1,n+1):
    list.append(int("{}".format(a)*i))
s = reduce(lambda x,y:x+y, list)
print list
print "The sum is calculated as:",s

The output is as follows:

a:4
n:4
[4, 44, 444, 4444]
Calculated sum: 4936

Python 3 test example:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input('n = '))
a = int(input('a = '))
sum = 0
total = 0
for i in range(n):
    sum += (10 ** i)
    total += sum * a
print(total)
    
#usr/bin/env python3
#coding:utf-8

n = int(input('Please enter the number of layers:'))
a = int(input('Please enter the number of calculations:'))
Su = 0
Tu = []
for i in range(n):
    Tu.append(int(str(a)*(i+1)))
print('The sequence created is:%s' % Tu)
Su = sum(Tu)
for i in Tu:
    Su = Su + i
print('The sum of the sequence is:%s' % Su)
    
#!/usr/bin/python
# -*- coding: UTF-8 -*-

def fadd():
    a = input("Input number:")
    loopNum = int(input("Number of digital cycles:"))
    sum = 0
    strsum  =""
    for i in range(loopNum):
        sum += int(str(a)*(i+1))
        strsum += str((str(a)*(i+1))) + "+"
    strsum = strsum[:len(strsum)-1] + "=" + str(sum)
    print strsum
fadd()
    
#!/usr/bin/python
# -*- coding: UTF-8 -*-

def calculate(a,n):
    sum = 0
    for i in range(1,n+1):
        x = int(str(a)*i)
        sum+=x
    print sum
calculate(4,4)
    
#!/usr/bin/python
# -*- coding: UTF-8 -*-

n = 4
c = 4

def num(n,time):
    'Splicing numbers, such as n =3,time = 4 Return to 3333'
    l = []
    while time > 0:
        l.append(str(n))
        time -= 1
    l = ''.join(l)
    return int(l)

sum = 0
for i in range(1,c + 1):
    j= num(n,i)
    sum = sum + j

print sum

Python 3 test example: use recursive function to generate the value of each item, and add:

#!/usr/bin/env python3

def f(n,a):
    if(n==1):
        return a
    else:
        r=f(n-1,a)*10+a
        return r

n=int(input("n=\n"))
a=int(input("a=\n"))
print("")
sum=0
for i in range(1,n+1):
    print(f(i,a))
    sum=sum+f(i,a)

print("sum=",sum)

Python 3 test case:

#!/usr/bin/python3

n=input('Please enter the calculation number:')
m=eval(input('Please enter the number of layers:'))
s=0
for i in range(1,m+1):
    a=n*i
    s+=eval(a)
print('{}'.format(s))
    
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import math

a=input("Please input a: ")
n=input("Please enter the number of numbers:")
r = 0
for i in range(0,int(n)):
    num=0
    for j in range(0,i+1):
        num += int(a) * int(math.pow(10,j))
    r += num
print(r)

Python 3 test:

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

def suman(a, n):
    if n<1:    return []
    s = [a]
    m = 10**len(str(a))
    for i in range(0, n-1):
        s.append(s[i]*m + a)
    return sum(s), s

print(suman(4,5))

Test under Python 3:

num=input('Input 1-10 Number between:')
times=int(input('frequency:'))
s=0
for i in range(1,times):
    print(num*i,'+',end='')
    s+=int(num*i)
print(num*times,'=',s+int(num*times))

Test under Python 3 to check whether the input is a number:

a=input('Please enter an integer:')
b=input('Please enter the times:')
d=0

if str.isdigit(a)and str.isdigit(b):
    for i in range(1,int(b)+1):
        c=a*i
        d+=int(c)
    print('And:'+str(d))
else:
    print('Please re-enter!')

Python 3 test:

for j in range(5):
    a = str(input("Tip: Please enter 1~9 Positive integer between:"))
    n = int(input("Tip: Please enter a positive integer greater than zero:"))
    if n <= 0 or int(a) >= 10 or int(a) <= 0:
        print("Please input the correct number according to the prompt")       
        continue
    sum = 0
    for i in range(1, n+1):
        print(int(a*i), end = ' ')
        sum += int(a*i)
    print("The sum of these numbers is:%d"%sum)
This small example is also helpful for practical application. If you feel good, please support me...

Tags: Python Lambda

Posted on Mon, 30 Mar 2020 21:46:53 -0700 by frih