Fam4k

🔰 Project Overview

The current image has no alternative text. The file name is: family.jpg

A calculator is one of the simplest yet most effective projects to start with in Python.
You will learn:

  • Taking user input.
  • Using functions for organization.
  • Handling basic operations (addition, subtraction, multiplication, division).
  • Using conditionals to control the logic.

✍️ Step 1: Plan the Calculator

We need:

  1. A menu for users to choose an operation.
  2. Functions for each operation.
  3. A loop to keep the calculator running until the user decides to quit.

🖥️ Step 2: Write the Code

# Basic Calculator in Python

# Define operations
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

def divide(a, b):
    if b != 0:
        return a / b
    else:
        return "Error! Division by zero."

# Main program loop
while True:
    print("\n📌 Simple Calculator")
    print("1. Addition (+)")
    print("2. Subtraction (-)")
    print("3. Multiplication (*)")
    print("4. Division (/)")
    print("5. Exit")

    choice = input("👉 Choose an operation (1-5): ")

    if choice == "5":
        print("✅ Exiting the calculator. Goodbye!")
        break

    if choice in ["1", "2", "3", "4"]:
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))

        if choice == "1":
            print("Result:", add(num1, num2))
        elif choice == "2":
            print("Result:", subtract(num1, num2))
        elif choice == "3":
            print("Result:", multiply(num1, num2))
        elif choice == "4":
            print("Result:", divide(num1, num2))
    else:
        print("⚠️ Invalid choice, please try again.")

🎯 Sample Output

📌 Simple Calculator
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exit
👉 Choose an operation (1-5): 1
Enter first number: 10
Enter second number: 5
Result: 15.0

📊 Operation Summary Table
OperationFunction UsedExample InputExample Output
Additionadd(a, b)10 + 515.0
Subtractionsubtract(a,b)10 – 55.0
Multiplicationmultiply(a,b)10 * 550.0
Divisiondivide(a,b)10 / 52.0
Division by 0divide(a,b)10 / 0Error message

🧠 Exercise for You
  • Add a power function (exponentiation).
  • Add a modulus function (remainder).
  • Improve the calculator by allowing the user to enter the operation as a symbol (+, -, *, /) instead of numbers.

📝 Summary

  • You learned how to build a basic calculator using Python.
  • You practiced functions, loops, conditionals, and input handling.
  • This project is the foundation for building more advanced tools like a scientific calculator.
⬇️ Download Fam4k تحميل

Related Posts

FlyFeditv

📘 Introduction In this project, you’ll create a command-line Expense Tracker that allows users to: ✅ Add expenses with category, amount, and description✅ View total spending and category breakdown✅ Automatically…

Read more

NetCinFly

📘 Introduction In this project, you’ll develop a Student Grades Analyzer in Python that can: This project helps you strengthen your skills with lists, dictionaries, loops, and data analysis logic….

Read more

Eagle_Pro

🌐 Introduction In this project, you’ll create a Web Scraper App in Python that extracts quotes, authors, and tags from a live website.You’ll use the BeautifulSoup and Requests libraries to…

Read more

Cobra_Pro

🌦️ Introduction In this project, you’ll build a Weather App in Python that retrieves live weather information from an online API.You’ll learn how to work with HTTP requests, JSON data,…

Read more

Show7-Pro

Concepts Covered: 🎯 Objective: Create a simple, text-based contact book application that allows users to: 💡 Code (contact_book.py): 🧠 Example Usage Console Output Example: 💾 Notes ⬇️ Download Show7-Pro تحميل…

Read more

Rapid_tv

🔹 Introduction The Rock, Paper, Scissors Game is a classic hand game that you can easily build in Python. 👉 Rules: 🔹 Code Example 🔹 Example Run 🔹 Concepts Learned…

Read more