-2

I am trying to add 2 integers in Python3 using input.

def sum(x,y):
    return x+y
a = int(input("Enter first number"))
b = int(input("Enter second number"))
print("The sum of a and b is", sum(a,b))

and get the following error

Traceback (most recent call last):
File "Main.py", line 7, in <module>
a = int(input("Enter first number"))
ValueError: invalid literal for int() with base 10: '1 1'

Another concern is this works normally in my Jupyter notebook, but for another online practice center it shows this error.

Shaner
  • 73
  • 1
  • 10
  • What is the input you are giving? It looks like you are trying to convert `'1 1'` to an `int`. – iz_ Jan 09 '19 at 05:13
  • the first step in debugging should be to split `int(input(..))` into two statements so that you can examine the result of `input(..)` to see if it's what you are assuming it is. – Bryan Oakley Jan 09 '19 at 05:14
  • You are not giving the right input mate. Giving input as 1 1 as Tomothy said, would obviously give you this error. Because Python is converting this input of 1 1 into integer and it can convert '11' to int but not '1 1'. Of course it's supposed to give this error. – Amit Amola Jan 09 '19 at 05:16
  • Possible duplicate of [ValueError: invalid literal for int () with base 10](https://stackoverflow.com/questions/13861594/valueerror-invalid-literal-for-int-with-base-10) – Amit Amola Jan 09 '19 at 05:17
  • @Tomothy32 The practice center is providing that input, it looks like two space separated integers? Is that a problem with this code? – Shaner Jan 09 '19 at 05:20
  • @AmitAmola The practice center inputs into my code, I don't think this is a duplicate of the other question as it is not a float number is it? – Shaner Jan 09 '19 at 05:22
  • If you see below that float answer, the answerer does mention about spaces as well. That's why I marked it as a duplicate. Now see, if the input is going to be like that only, then you can use .replace(" ","") and this would work perfectly fine I believe. – Amit Amola Jan 09 '19 at 05:40

3 Answers3

0

Your code is working, but not for the specific input the practice center is giving. Make this modification:

nums = [int(x) for x in input("Enter numbers: ").split()]
print("The sum of a and b is", sum(nums))

By the way, sum is a built-in function, so you don't have to write it yourself. The only line that really changed is this:

nums = [int(x) for x in input("Enter numbers: ").split()]

nums will be a list of numbers, as the name implies. The next part is the list comprehension. input("Enter numbers: ").split() will take the input and split it on any whitespace. For instance, 'hello world' will be turned into a list with ['hello', 'world']. In this case, '1 1' will be turned into a list with ['1', '1']. Then, with the list comprehension, you are turning each element into an integer (['1', '1'] -> [1, 1]). Then, you pass this list into sum. Also, this does the same thing as the list comprehension:

nums = list(map(int, input("Enter numbers: ").split()))

It doesn't matter which one you choose. If you wanted to get real fancy, you can do the whole thing in one line:

print("The sum of a and b is", sum(map(int, input("Enter numbers: ").split())))
iz_
  • 15,923
  • 3
  • 25
  • 40
0

Given that your input are coming with spaces itself, you can use replace command and replace those spaces.

def sum(x,y):
    return x+y

a = int(input("Enter first number: ").replace(" ",""))
b = int(input("Enter second number: ").replace(" ",""))
print("The sum of a and b is: ", sum(a,b))

For your specific case, this should work. What I am doing here is that I am converting the input like '8 3 525 5', to '835255', which itself will be easily converted to int later on and will work perfectly.

Removing the duplicate as well mate.

Amit Amola
  • 2,301
  • 2
  • 22
  • 37
0

If you want to input all values in the same line as 1 1, then you should use split():

def sum(x,y):
    return x+y

a, b = map(int, input("Enter numbers ").strip().split())
print("The sum of a and b is", sum(a,b))

Output:

C:\Users\Desktop>py xxx.py
Enter numbers 1 1
The sum of a and b is 2

If separately values need to be inputted:

def sum(x,y):
    return x+y

a = int(input("Enter first number ").strip())
b = int(input("Enter second number ").strip())

print("The sum of a and b is", sum(a,b))

Output:

C:\Users\Desktop>py xxx.py
Enter numbers 1 1
The sum of a and b is 2
Rarblack
  • 4,559
  • 4
  • 22
  • 33