2. Add two numbers
Your program reads two lines of input. Each line holds one whole number (an integer), which can be negative or zero. Print the sum of the two numbers.
input() always gives you text, so turn each line into a number before adding.
Example 1
Input
3
4Output
7Explanation3 + 4 = 7.
Example 2
Input
10
-2Output
8Explanation10 + (-2) = 8.
Constraints
- Each number is a whole number from -1,000,000,000 to 1,000,000,000.
- Each line holds only the number, with no spaces around it.
- Print nothing else. A prompt inside
input()is fine: the tests leave it out of the output.
Submit also runs 3 more tests you can't see.
Practises User Input (lesson 6).
input()always gives you a string, and adding two strings joins them:"3" + "4"is"34", not7.int(...)turns text into a whole number, minus sign included:int("-2")is-2.- Read and convert each line in one go with
a = int(input()), thenprint(a + b).
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Was this challenge helpful?
Thanks!
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
Code
# Read two whole numbers, each on its own line. # Print their sum.
Run your code to see how it does on the test cases, or Submit to check every test.