Thuta Learning
AdvancedProgrammingbeginner

Add Two Numbers (Enhanced)

Relax. We'll talk through this in plain words — no textbook voice.

➕ Lesson 59: Add Two Numbers (Basic Arithmetic & User Input)

1. Problem Overview

In short → In Python, we want the user to enter two numbers, add them together, and show the result.

In other words → The task is to take two numbers as input from the user, add them, and display the result.

2. Methods

  • Static numbers → direct assignment
  • User input → input() function
  • Function-based → reusable code
  • Lambda function → one-line solution

3. Summary

✅ Basic arithmetic → addition operator (+)

✅ User input → input() + int() conversion

✅ Function-based → reusable, clean code

✅ Error handling → validate numeric input

python
# ===== 1. Using Static Numbers =====
a = 5
b = 7
result = a + b
print(f"{a} + {b} = {result}")

# ===== 2. Using User Input =====
print(f"\n===== User Input =====")
# In real app: a = int(input("Enter first number: "))
# In real app: b = int(input("Enter second number: "))
# Demo with fixed values:
a = 10
b = 20
result = a + b
print(f"Enter first number: {a}")
print(f"Enter second number: {b}")
print(f"The sum is: {result}")

# ===== 3. Using Function =====
print(f"\n===== Using Function =====")

def add_numbers(x, y):
    return x + y

num1 = 15
num2 = 25
print(f"The sum is: {add_numbers(num1, num2)}")

# ===== 4. Using Lambda Function =====
print(f"\n===== Using Lambda =====")

add = lambda x, y: x + y
print(f"Lambda: {add(30, 40)}")

# ===== 5. Error Handling =====
print(f"\n===== Error Handling =====")

def safe_add():
    try:
        # In real app: a = int(input("Enter first number: "))
        # In real app: b = int(input("Enter second number: "))
        a, b = 50, 60
        result = a + b
        return f"The sum is: {result}"
    except ValueError:
        return "Error: Please enter valid numbers!"

print(safe_add())
You should see
5 + 7 = 12 ===== User Input ===== Enter first number: 10 Enter second number: 20 The sum is: 30 ===== Using Function ===== The sum is: 40 ===== Using Lambda ===== Lambda: 70 ===== Error Handling ===== The sum is: 110
Add Two Numbers (Enhanced) | Thuta Learning