Objective
Create an interactive program that takes in two integers and outputs their sum.
Difficulty
Description
In this exercise, you have to create a program that asks the user to input two integers, one-by-one, and then outputs their sum.
The first input prompt of the program should be of the form shown below:
x: <input>
Here, <input>
denotes the input of the user.
Similarly, the second input should be of the form:
y: <input>
The output should be:
The sum is: <sum>
where <sum>
is the sum of the input integers.
Shown below is an example:
New file
Inside the directory you created for this course on Python, create a new folder called Exercise-1-Addition-Calculator and put the .py solution files for this exercise within it.
Solution
As instructed in the question, first we need to ask the user to input a value for x
and then a value for y
, with the prompts 'x: '
and 'y: '
, respectively.
x = input('x: ')
y = input('y: ')
a
and b
, but since the prompt messages specifically use the names 'x'
and 'y'
, it would be straightforward to go with these names for the variables as well.Next, up we need to convert both x
and y
to integers using the function int()
.
x = input('x: ')
y = input('y: ')
# convert x to int
x = int(x)
# convert y to int
y = int(y)
Finally, we just ought to add x
and y
together and print out the result, as desired in the exercise.
x = input('x: ')
y = input('y: ')
# convert x to int
x = int(x)
# convert y to int
y = int(y)
print('The sum is:', x + y)
Below, we use this program to add two numbers: