2

I wanted to do the following simple calculation by passing values for the parameters num1 and num2 from input() methods.

I tried following code:

def add(num1, num2):
    return num1 * num2

num1 = input('Enter number1: ')
num2 = input('Enter number2: ')

print(add(num1, num2))

But it is showing the following error when it is run (After input num1 and num2):

TypeError: can't multiply sequence by non-int of type 'str'

Can somebody please explain where did I go wrong and how to convert an input string to the integer type?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Indi
  • 421
  • 9
  • 28
  • 3
    Just a comment on intentions: are you sure a function named `add` should perform multiplication? – Thomas Oct 21 '19 at 06:30

4 Answers4

6

input() will returns a string, you have to convert the string to a int or float, you can test if the input is a valid number.

isnumeric() will return true if all chars in the string are numeric ones and the string is not empty.

Note: i renamed the function to multiply because this is not adding ...

def multiply(num1,num2):
    return num1*num2

inp1=input('Enter number1: ')
inp2=input('Enter number2: ')

if inp1.isnumeric() and inp2.isnumeric():
    num1 = int(inp1)
    num2 = int(inp2)
    print(multiply(num1,num2))
else:
    print("Atleast one input is not numeric")
Fabian
  • 1,130
  • 9
  • 25
  • 1
    Thank you for the answer and the additional tip! It will be very useful! – Indi Oct 21 '19 at 07:05
  • 1
    @Upz Please see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Tomerikoo Apr 03 '21 at 08:40
4

You can try this:

def add(num1, num2):
    return num1 * num2

num1 = int(input('Enter number1: '))
num2 = int(input('Enter number2: '))

print(add(num1, num2))

The input function stores the input as a string and so what is happening in your code is that you are entering two integers but they are being stored as strings. You cannot multiply two strings. All you need to do is convert the input strings to integers by using the [int()][2] function. If you want to multiply floats, you can use the [float()][3] function in place of the int() function. You can also convert the strings to integers or floats once you have passed them into the function. Something like this:

def add(num1, num2):
    return int(num1) * int(num2)

num1 = input('Enter number1: ')
num2 = input('Enter number2: ')

print(add(num1, num2))
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Sultan Singh Atwal
  • 810
  • 2
  • 8
  • 19
1

input (in Python 3) returns a str which is seen as text and not a number. If you want to multiply (or add) numbers, you must first parse the strings as numbers.

Sometimes this will fail as well. If you're assuming the input is always going to be a valid number, you can convert a string using:

float("3.1415926")

You can use this in your code with:

def add(num1,num2):
    return num1*num2

num1=float(input('Enter number1: '))
num2=float(input('Enter number2: '))

print(add(num1,num2))

To avoid floating point errors, you can print/display the float using an f-string (added in Python 3.6).

def add(num1,num2):
    return num1*num2

num1=float(input('Enter number1: '))
num2=float(input('Enter number2: '))

print(f"{add(num1,num2):.2f}")
Thomas
  • 701
  • 2
  • 8
  • 23
  • 1
    I tried it and it worked! I have little doubt. When I changed the input type as float (randomly put values as num1=1.34 and num2=0.11), the result was, "1.4500000000000002". What is the reason for this value instead of just 1.45? – Indi Oct 21 '19 at 06:41
  • 2
    This is a classic computer science problem called [floating point error](https://en.wikipedia.org/wiki/Floating_point_error_mitigation). You can think about the number being added as "1.450...02" and choose to display the float using a limited number of decimal places. `print(f"{add(num1,num2):.2f})`. This assumes Python >= 3.6. – Thomas Oct 21 '19 at 07:13
-1
def mul(num1,num2):
    return(num1*num2)

inp1 = input('Enter number1:')
inp2 = input('Enter number2:')

num1 = int(inp1)
num2 = int(inp2)

print(mul(num1,num2))
azro
  • 53,056
  • 7
  • 34
  • 70