0

Hello everyone I started learning a wonderful programming language like Python today and immediately ran into an unusual problem. Perhaps you will find it too simple, so I apologize in advance for this question. The problem is this: I wrote a program that receives 2 numbers and outputs their sum. It seems the task is simple, but the output is a little not what I expect... Here is a part of my program where i'm trying to do it:

a = input()
b = input()
print(a + b)

However, the above code does not work correctly. If you run this program and input the numbers 1 and 2, then instead of the expected output '3' we get '12'. After analyzing this, I understand that there is a string concatenation instead of adding the numbers. But why did Python define the values I entered as strings? I thought Python was a "dynamic type" language.

1 Answers1

0

input() always returns strings as of Python 3.

Explicitly cast to integers:

a = int(input('a: '))
b = int(input('b: '))
print(a + b)
AKX
  • 152,115
  • 15
  • 115
  • 172