-1
print("Give me two numbers and I'll add them for you\n")

num_1 = input("Enter first number: ")
num_2 = input("Enter second number: ")

answer = int(num_1 + num_2)
print(answer)

The answer I get when I add 5 + 4 is 54.

Why the code failed.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
Steev
  • 1
  • 1
  • 2
    `input` returns a string, even if it is a string of digits. – Scott Hunter Jun 22 '23 at 13:43
  • you need to add two object of integer type not str type. input make an object str type, so str + str = str and int + int = int type, you need to convert both number object to int first and then add them – sahasrara62 Jun 22 '23 at 13:46

2 Answers2

2

The input() function returns result as a str value.

"+" operation for str means concatenation, so '123' + '4' is '1234'

When you call int() function for '5' + '4' = '54' you get 54 as a result.

Correct solution is

print("Give me two numbers and I'll add them for you\n")

num_1 = int(input("Enter first number: "))
num_2 = int(input("Enter second number: "))

answer = num_1 + num_2 # this is integer value
print(answer)
Renzog
  • 21
  • 2
0

num1 and num2 are strings, cast them and then perform the sum.

answer = int(num_1) + int(num_2)