[Job] Python- package function: input x = 2, y = 5, 2 Evaluates + 222 + 22 + 22222 + 2222

1.1 wrapper function to achieve the following claims

  For example: Enter 2,5

  Evaluates: 2 + 22 + 222 + 2222 + 22222 and

  First, we labeled the answer:

 

  The first thought of a super-stupid way code is as follows:

# Defined Functions 
DEF func_sum (x, Y):
     # put into a string assigned to x c, convenient operation 
    C = STR (x)
    S = 0
     # define an empty entry list later used to store y X 
    list_num = []
     # traversal key y x, and adds it to the list stored 
    for I in Range (. 1,. 1 + y ):
        c *= i
        list_num.append(c)
        # It should be restored back to c, or there will be a lot of stitching together c 
        c = str (the X-)
     # print of your list to see if we need to list 
    Print (list_num)
     for A in list_num:
         # The extracted characters turn conversion an int 
        B = int (A)
         # accumulate operation 
        S + = B
     # returns the final result 
    Print (S)

# Call the function 
func_sum (2, 5)

  Output is as follows:

 

   Y is a schematic idea items need to calculate x superposed sequentially added to the list and then taken out through the list by adding the value

  Then bored when they looked at their code, and then simplify it:

# Suddenly zero sense came, they think of a way 
DEF get_sum (the X-, the y-):
    sum1 = 0
    c = str(x)
    for n in range(0, y):  # 0 1 2
        a = n * c + c  # n = 0 a = 3  n = 1 c =33  n = 2  a = 333
        x = int(a)
        sum1 += x  # sum1 = 3   33   333
    return sum1

# Call the function 
Print (get_sum (3, 3))

 

  Output is as follows:

 

 to be continued...

 

 

 

Guess you like

Origin www.cnblogs.com/cxstudypython/p/11965506.html