'Adding 2 value using numpy

I am trying to write a an input code that can put 2 output all together and add it up. The code is below;

import numpy as np
x, y = input("Insert value").split()

print(x)
print(y)
print(np.add(x,y))

Output:

4
3

---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-10-d7826b294c37> in <module>
      4 print(x)
      5 print(y)
----> 6 print(np.add(x,y))

UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U1')) -> dtype('<U1'

Can anyone help this?



Solution 1:[1]

You have not told Python to convert your inputs, which are strings, into numbers. If your typecast to integers, your code will work:

import numpy as np
x, y = input("Insert value").split()

print(x)
print(y)
print(np.add(int(x),int(y)))

Solution 2:[2]

The answer is simple. Just add int() on every variable and use '+' operator.

import numpy as np
x, y = input("Insert value").split()

print(x)
print(y)
print(int(x) + int(y))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hoog
Solution 2 xk derhaka