Package function to achieve the following claims
For example: Enter 2,5
Evaluates: 2 + 22 + 222 + 2222 + 22222 and
Method One: cycle recursive call
int = X (INPUT ( " Enter a number 1-9 " )) Y = int (INPUT ( " Please enter a positive integer " )) SUM1 = 0 B = Y for A in Range (. 1,. 1 + B ): DEF F (Y): IF Y == 0: return 0 return X * 10 ** (. 1-Y) + F (. 1-Y ) SUM1 + = F (Y) Y = Y. 1- Print (SUM1)
Output results:
1- Please enter a number of 2 9 enter a positive integer. 5 24690 Process Finished Exit with code 0
Method two: decorator Package
int = A (INPUT ( " Enter a number 1-9 " )) B = int (INPUT ( " Please enter a positive integer " )) DEF F1 (F): DEF F2 (X, Y): Str = "" for I in Range (. 1,. 1 + Y ): Str + STR = (X) * I IF I == Y: Str + = " = " the else : Str + = " + " the Sum = 0 for j in range(1,y+1): Sum += int(str(x)*j) return f(Str,Sum) return f2 @f1 def f(a,b): print(a,b) f(a,b)
Output results:
1- Please enter a number of 2 9 enter a positive integer. 5 2 + 22 is 2222 + 222 + 24690 + 22222 = Process Finished Exit with code 0