Home > other >  Using 2 input in one line to add up the value
Using 2 input in one line to add up the value

Time:11-15

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'

I try to add add up 3 and 4 using numpy function, add, but it show and error. Can anyone help this? How can I add up both output using this kind of input code?

CodePudding user response:

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)))

CodePudding user response:

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))

  • Related