-1

I am taking a course for Python, and essentially there's this code:

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
result = num1 + num2

print(result)

which is supposed to take num1 and num2 and add them together, but instead of adding, the program concatenates the result. So for example if num1 = 5 and num2 = 9, the result would be 59 and not 14.

Emre Sevinç
  • 8,211
  • 14
  • 64
  • 105
  • that's because the `+` operator concatenates strings – C8H10N4O2 Feb 07 '20 at 15:15
  • I think this is a duplicate of https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers – Rahul P Feb 07 '20 at 15:18
  • When we use `+` with string python concatenates the strings SO first convert `num1` & `num2` into `int`, Input function of Python take input as a String. – waqas ahmad Feb 07 '20 at 15:21
  • Do you take a course for Python 2? Don't do that. Python 2 has already reached end of life. Switch to Python 3. – Matthias Feb 07 '20 at 16:14

1 Answers1

3

You need to do int(num1) + int(num2) because num1 and num2 are strings.

Note that "foo" + "bar" returns "foobar", since + used on strings will concatenate them.

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134