-3
num1 = input("First number : ")
num2 = input("Second number : ")

sum = num1 + num2 

print (sum)

#output 

#First number : 1

#Second number : 2

#12

what is my error (first number is 1 and second number is 2 = 3 but output says 1 + 2 = 12)

2 Answers2

0

You Should try Type conversion

num1 = int(input("First number : "))
num2 = int(input("Second number : "))
sum = num1 + num2 
print (sum)
0

Problem is the input will be considered as string as per your program, you have to do a little modification in your program. Just add int before input and it'll do the work.

num1 = int(input("Enter number one: "))
num2 = int(input("Enter number two: "))
print(num1+num2)

There is no error, the output is just concatenated num1 and num2, you need integers to perform addition.

You can understand it clearly in interactive shell

>>>"1"+"2" #your code is doing the same
>>>12
>>>1+2 #this is what you need to do
>>>3
Damian Wayne
  • 1
  • 1
  • 3
  • Nice explanation. Maybe it would help to add some code snippet when you said `Just add int before input and it'll do the work` having `num1 = int(input("First number : "))` could make your answer clearer – Al-un Mar 05 '19 at 10:31
  • 1
    It's my first answer, thanks for pointing out. – Damian Wayne Mar 05 '19 at 16:38