0

I'm newbie for the Python. Here I'm trying to add two value by using

x = 10
y= 20
print("Addition value is ", x+y )

It's return the proper result as: Addition value is 30.

But while I'm read the input by the user by using input() . It's just concatenate the x and y value. For ex)

x = input('Enter first number:')
y = input('Enter the second number')
z = x + y
print("The addition value is:",z)

Imagine if I give user input for first number as 10 second number as 20 means

It's return the result as "The addition value is: 1020

I'm not sure why it's concatenate the two values instead of adding two values Is I did any mistake on my code. Please correct me. Thanks in advance.

Sociopath
  • 13,068
  • 19
  • 47
  • 75
Kannan.P
  • 1,263
  • 7
  • 14

1 Answers1

0

Just change your code to:

x = int(input('Enter first number:'))
y = int(input('Enter the second number'))
z = x + y
print("The addition value is:",z)

Reasoning:

The type of user input(input method) is always string. If you perform + on strings it will concatenate both values instead of adding. So first convert them into int(or float)

Sociopath
  • 13,068
  • 19
  • 47
  • 75