-1
x = input("Enter the first number!")
y = input("Enter the second number!")
z = input("Enter the third number!")

def adding(a, b, c):
    s = a+b+c
    return s

c = adding(x, y, z)
print(c)

I've tried a program of addition in python IDE, but instead of printing sum it returns concatenation of numbers as strings.

I don't know what's wrong with it?

Anybody have any idea?

Sorry! for a dumb question.....

Gaurav Tomer
  • 721
  • 2
  • 9
  • 25
  • possible duplicate of [How can I read inputs as integers in Python?](http://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers-in-python) – TigerhawkT3 Jul 31 '15 at 04:32
  • You just need to cast your input to an integer. Use `int(input("Enter your number!"))`, though this won't handle errors. – fallaciousreasoning Jul 31 '15 at 04:52

3 Answers3

6

input by default should get the characters of type string. You need to convert them to integers. If those are floating point numbers, then change int in the below code to float.

def adding(a, b, c):
    s = int(a)+int(b)+int(c)
    return s
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
3

Your problem is that your input is in a string form and you need it to be in an int (or float) form.

def input_number(message):
    return int(input(message))

x = input_number("Enter the first number!")
y = input_number("Enter the second number!")
z = input_number("Enter the third number!")

def adding(a, b, c):
    s = a+b+c
    return s

c = adding(x, y, z)
print(c)

Should work nicely for you.

Note: This won't handle errors (say, if the user enters something that isn't a number). If you want your numbers to have decimals simply change the int in input_number to a float.

0

so basically python take all the input as String we have to convert explicit-ally, in the below code i am converting input as int(casting it) Using this example you can take the reference for the above problem.

def get_sum(data1,data2):
    """this function is responsible to do sum opration of two number , and return back to coller"""
    return (data1+data2)

def get_as_number_int(msg):
    """this function is responsible to take input as integer and retunr back to coller"""
    return int(input(msg))

data1=get_as_number_int("Please enter data1 value")
data2=get_as_number_int("Please enter data2 value")

print("sum of input : ",get_sum(data1,data2))