-1

I am a beginner in Python and cannot understand the below code. Somebody please explain the execution flow.

The Question is to enter a number 'N' and calculate N+NN+NNN.

  a = int(input("Input an integer : ")) 
  n1 = int( "%s" % a )
  n2 = int( "%s%s" % (a,a) )
  n3 = int( "%s%s%s" % (a,a,a) )
  print (n1+n2+n3)
Gahan
  • 4,075
  • 4
  • 24
  • 44

4 Answers4

0

It simply does the task of the question- TO ENTER A NUMBER 'N' AND CALCULATE 'N+NN+NNN
So first line it stores the input number in a variable 'a'.
Then in next line it simply passes the value of a as an integer to n1.
In the next line(as the question wants NN of the input number N)it just uses %s token to insert it as a string so a now becomes aa then it converts it to integer by using the int() method. Same goes with line 3.
Then the print line just prints the sum of the three values n1,n2 and n3.

Dragon
  • 1,194
  • 17
  • 24
0

Your code does the following.

a = int(input("Input an integer : ")) 
  n1 = int( "%s" % a )
  n2 = int( "%s%s" % (a,a) )
  n3 = int( "%s%s%s" % (a,a,a) )
  print (n1+n2+n3)

Line 1 - It displays 'Input the integer:" and stores the input in variable a.
Line 2 - n1 stores the value of variable a.
Line 3- n2 stores the value of variable 'aa' as integer.
Line 4 - n3 stores the value of variable 'aaa' as integer.
Line 5 - It adds the values present in n1, n2, n3 and prints it.

Here %s is the formatting string which is replaced by the value present at the end after % sign. For more details on this, visit here

Mithilesh_Kunal
  • 873
  • 7
  • 12
0
a = int(input("Input an integer : ")) # takes integer as input and stores it as integer in variable a # consider input as 7
n1 = int( "%s" % a ) # when "%s" will give you result "7" and it is stored in n1 as integer 7 because int() is used as int("7")
n2 = int( "%s%s" % (a,a) ) # similarly when %s is written twice it requires two numbers in this case it is same hence you will get "77" to converted to integer and stored in n2
n3 = int( "%s%s%s" % (a,a,a) ) # just like n2 number is used three times instead of two so you will get n3=777 in this case
print (n1+n2+n3) # as all values of n1,n2 and n3 are integer it can be added 7+77+777 and you will get result 861

You can achieve same with many approach below is another attempt to it:

a = input("Input an integer : ") # gets any input but if you need validation you can try it using while loop and if condition or try block
aa = a+a # in python when two string are concatnated i.e. "71"+"71" result will be like "7171"
aaa = a+a+a # concatnated three string
print(int(a)+int(aa)+int(aaa)) # Note: if input won't be a number this will throw ValueError

You can learn more at official documentation of string formatting

Gahan
  • 4,075
  • 4
  • 24
  • 44
0

The code you're showing is a very clumsy way of doing the sum of numbers composed of 1 to 3 digits of a number.

Just for fun, with a generator comprehension fed to sum, and using string multiplication to generate a string composed of the same digit/number 1 to 3 times:

a = input("Input an integer : ")  # python 2 would need raw_input or the result would be incorrect
print (sum(int(a*i) for i in range(1,4)))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219