Project Overview 💡
In this project, you’ll learn how to take user input, convert it into numbers, and calculate their sum. This is an essential skill for handling user interactions in Python programs.
Challenge Yourself! 🚀
Before looking at the solution, try to solve this problem on your own!
Task:
Write a Python script that asks the user for two numbers, converts them to integers, sums them up, and prints the result.
Expected Output:
The program should ask for two numbers and display their sum. In the following example, the user entered 12 and then 4. The program prints out the output in the last line.
Give it a shot! Scroll down when you’re ready for the complete step-by-step guide.
Spoiler Alert!
Step-by-Step Guide
Step 1️⃣: Get User Input
First, prompt the user to enter two numbers. The input()
function takes input as a string.
Place this code in line 1-3:
# Get user input
first_number = input("Enter the first number: ")
second_number = input("Enter the second number: ")
Step 2️⃣: Convert Inputs to Integers
Since input()
returns a string, we need to convert the inputs into integers using the int()
function.
Place this code in lines 5-7:
# Convert inputs to integers
first_number = int(first_number)
second_number = int(second_number)
Step 3️⃣: Calculate the Sum
Now, add the two numbers and store the result in a variable.
Place this code in line 9-10:
# Step 3: Calculate the sum
result = first_number + second_number
Step 4️⃣: Display the Result
Finally, print the sum.
Place this code in line 12-13:
# Display the result
print("The sum of the two numbers is:", result)
Complete Code 🧨
Original Solution:
# Step 1: Get user input
first_number = input("Enter the first number: ")
second_number = input("Enter the second number: ")
# Step 2: Convert inputs to integers
first_number = int(first_number)
second_number = int(second_number)
# Step 3: Calculate the sum
result = first_number + second_number
# Step 4: Display the result
print("The sum of the two numbers is:", result)
Alternative Solution
Another way to achieve the same result is by converting user input into integers right when receiving it:
# Alternative method: Convert input to integers immediately
first_number = int(input("Enter the first number: "))
second_number = int(input("Enter the second number: "))
# Calculate and print the sum
print("The sum of the two numbers is:", first_number + second_number)
Comparison of Both Approaches:
Original Approach: First stores user input as strings, then converts them to integers separately. This makes debugging easier since each step is explicit.
Alternative Approach: Converts input directly inside int()
, reducing the number of variables and making the code more compact. However, if an error occurs (e.g., the user enters text instead of a number), it may be harder to identify which input caused the problem.
Both solutions are valid—use whichever fits your style best!
Extra Challenge 🚀
Modify the script to handle cases where the user enters non-numeric values. Use a try-except
block to catch errors and ask the user to enter numbers again.
Example interaction:
Enjoyed this project? Unlock real-world projects with full guides & solutions by subscribing to the paid plan.
Thank you sir