Mini Exercise: Sum Two Numbers in Python

Introduction

Great progress so far. In this chapter, you will build your first small interactive Python feature: ask the user for two numbers and return their sum. This exercise is simple, but it combines variables, operators, and input handling, which means you are already writing real program logic.

Prerequisites

  • Python 3.10+ installed
  • Basic understanding of variables and operators
  • Ability to run .py files in terminal or IDE

What You Will Build

You will build a console program that:

  1. Prompts the user to enter two numbers
  2. Converts input text into numeric values
  3. Calculates the sum
  4. Prints the result clearly

Output goal:

  • User enters 3 and 5
  • Program returns 8

Tip

You Are Doing Real Programming

This is not "just a beginner demo."
Input -> processing -> output is a core software pattern used in real systems.

Step 1: Write the Basic Version

Create a file named sum_two_numbers.py and add:

python
# Ask user for the first number
num1 = float(input("Enter the first number: "))
 
# Ask user for the second number
num2 = float(input("Enter the second number: "))
 
# Calculate the sum
result = num1 + num2
 
# Print the final result
print(f"The sum is: {result}")

Run it:

bash
python sum_two_numbers.py

Example:

  • Input: 10 and 25
  • Output: The sum is: 35.0

Step 2: Understand Why float() Is Needed

input() always returns text (str), even when users type digits.

Without conversion, this happens:

python
# Both values are text
a = input("A: ")
b = input("B: ")
 
# This concatenates strings instead of numeric addition
print(a + b)

If user enters 2 and 3, output becomes 23, not 5.

By using float() or int(), you tell Python to do math, not string joining.

Step 3: Make Output Friendlier

You can improve user experience with clearer prompts and formatting:

python
# Ask for numbers with friendly prompts
first_number = float(input("Please enter number 1: "))
second_number = float(input("Please enter number 2: "))
 
# Compute result
sum_result = first_number + second_number
 
# Show readable output
print(f"Awesome! {first_number} + {second_number} = {sum_result}")

This kind of wording gives learners positive feedback and makes console tools feel better.

Beginners often type invalid input by accident. You can handle that safely:

python
try:
    # Read and convert the first number
    num1 = float(input("Enter the first number: "))
 
    # Read and convert the second number
    num2 = float(input("Enter the second number: "))
 
    # Calculate result
    result = num1 + num2
 
    # Print success message
    print(f"Great job! The sum is: {result}")
except ValueError:
    # Handle non-numeric input
    print("Oops! Please enter valid numbers only.")

Warning

If users enter letters like abc, numeric conversion will fail.
Use try/except to keep your program stable and user-friendly.

Practice Challenges

Try these small upgrades:

  • Show result with two decimal places
  • Repeat calculation in a loop until user chooses to exit
  • Print both sum and average

Challenge example (formatting):

python
# Print result with 2 decimal places
print(f"The sum is: {result:.2f}")

Confidence Check

If you completed this chapter, you already know how to:

  • Collect user input
  • Convert data types
  • Perform arithmetic operations
  • Print structured output
  • Handle simple runtime errors

That is a big milestone. You are moving from "writing lines" to "building behavior."

FAQ

Why do I get ValueError when input looks like text?

Because float() and int() require numeric text. If the input contains letters or invalid symbols, conversion fails.

Should I use int() or float() for this exercise?

Use int() for whole numbers only. Use float() if decimals are allowed.

Why is my output 35.0 instead of 35?

Because float() creates floating-point values, and Python preserves that type in the result.

Is this tiny program really important?

Yes. This pattern (input -> transform -> output) appears in scripts, APIs, services, and data workflows.