-1

Write a program allowing user to enter 2 integers, then calculate and print out their sum.

Input: A single line consists of 2 integers a and b, separated by a space. a and b are 32-bit integers, and it is guaranteed that their sum also is.

Output: Print the result following the format "a + b = c" of where c is the sum of a and b.

I have tried to do the code below.

# Reads two numbers from input and typecasts them to int using  
# map function 
a, b = map(int, input(4 5).split())
c = a + b
print('{0} + {1} = {2}'.format(a, b, c))

I expect the output format is "a + b = c"

Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32

3 Answers3

2

input function take one parameter to display the text on console.

input('enter two numbers') it will display the 'enter two numbers' on console, now you enter two number.

Just change your code like this

# Reads two numbers from input and typecasts them to int using  
# map function 
a, b = map(int, input('enter two numbers\n').split())
c = a + b
print('{0} + {1} = {2}'.format(a, b, c))

You will see output like

>>> a, b = map(int, input('enter two numbers\n').split())
enter two numbers
4 5
>>> a
4
>>> b
5
>>> c = a + b
>>> print('{0} + {1} = {2}'.format(a, b, c))
4 + 5 = 9
>>> 
Saleem Ali
  • 1,363
  • 11
  • 21
  • I tried your code, and it went like this. stdin Standard input is empty stdout enter two numbers enter two numbers stderr Traceback (most recent call last): File "prog.py", line 3, in EOFError: EOF when reading a line – Min Nguyen Sep 11 '19 at 05:35
  • @MinNguyen , can you paste complete steps and traceback in readable format – Saleem Ali Sep 11 '19 at 05:44
  • # Reads two numbers from input and typecasts them to int using # map function a, b = map(int, input('enter two numbers\n').split()) c = a + b print('{0} + {1} = {2}'.format(a, b, c)) – Min Nguyen Sep 11 '19 at 06:05
  • Traceback (most recent call last): File "/home/45250da2deebf23873cc26b36b9142c3.py", line 3, in a, b = map(int, input('enter two numbers\n').split()) EOFError: EOF when reading a line – Min Nguyen Sep 11 '19 at 06:06
  • @MinNguyen are you running this in any IDE like sublime?. Because I don't think Sublime Text's default console is that it does not support input. Can you try it with python consloe like `python3 file_path.py` – Saleem Ali Sep 11 '19 at 06:18
0

The input() function takes one argument, which is the text that is displayed when the user is prompted to enter something. Your code works fine, the only problem is that you didn't give the function a proper string to work with.

This code will work:

a, b = map(int, input("How many candies do they have? ").split())
c = a + b
print('{0} + {1} = {2}'.format(a, b, c))
Zciurus
  • 786
  • 4
  • 23
-1

try this:

a, b = map(int, input('Input ').split(' '))
c = a + b
print('{0} + {1} = {2}'.format(a, b, c))
Ruan
  • 219
  • 5
  • 9