Skip to main content

In this educational escapade, we’re diving into the world of mathematics and programming with the Raspberry Pi. Get ready to flex your computational muscles as we explore the creation of a Pi Calculator—a simple project designed to introduce you to the fundamental concepts of Python programming on your Raspberry Pi. Let’s embark on this mathematical journey and unlock the potential of your mini-computer!

Materials Needed:

  1. Raspberry Pi (any model with Raspbian OS installed)
  2. MicroSD card with Raspbian OS installed
  3. Power supply, keyboard, mouse, and monitor for your Raspberry Pi

Step 1: Set Up Your Raspberry Pi

If you haven’t set up your Raspberry Pi yet, follow the instructions on the official Raspberry Pi website to install the Raspbian OS.

Step 2: Open the Python IDE

  1. Once your Raspberry Pi is up and running, open the Python IDE. You can find it in the “Programming” section of the main menu.
  2. The Python IDE provides a user-friendly environment for writing and executing Python scripts.

Step 3: Write Your Pi Calculator Script

In the Python IDE, create a new file by clicking on “File” and then “New”. In the editor, type the following Python script:

# Pi Calculator

# Import the math module for mathematical operations
import math

# Function to calculate Pi using the Leibniz formula
def calculate_pi_leibniz(terms):
    pi = 0
    for i in range(terms):
        pi += 4 * ((-1) ** i) / (2 * i + 1)
    return pi

# Function to calculate Pi using the math module
def calculate_pi_math():
    return math.pi

# Function to perform basic mathematical operations
def basic_calculator():
    # Get user input for numbers
    num1 = float(input("Enter the first number: "))
    num2 = float(input("Enter the second number: "))

    # Perform basic operations
    print("\\\\nBasic Math Operations:")
    print("1. Addition (+):", num1 + num2)
    print("2. Subtraction (-):", num1 - num2)
    print("3. Multiplication (*):", num1 * num2)
    print("4. Division (/):", num1 / num2)

# Function to display Pi-related options
def pi_calculator():
    print("\\\\nPi Calculator Options:")
    print("1. Calculate Pi using the Leibniz formula")
    print("2. Get the value of Pi using the math module")

# Main program
if __name__ == "__main__":
    print("Welcome to the Pi Calculator!")

    while True:
        print("\\\\nOptions:")
        print("1. Basic Calculator")
        print("2. Pi Calculator")
        print("3. Exit")

        choice = input("Enter your choice (1/2/3): ")

        if choice == "1":
            basic_calculator()
        elif choice == "2":
            pi_calculator()
            sub_choice = input("Enter your choice (1/2): ")

            if sub_choice == "1":
                terms = int(input("Enter the number of terms to calculate Pi: "))
                result = calculate_pi_leibniz(terms)
                print(f"Approximate value of Pi using Leibniz formula: {result}")
            elif sub_choice == "2":
                result = calculate_pi_math()
                print(f"Value of Pi using the math module: {result}")
            else:
                print("Invalid choice. Returning to main menu.")
        elif choice == "3":
            print("Exiting Pi Calculator. Goodbye!")
            break
        else:
            print("Invalid choice. Please enter 1, 2, or 3.")

Step 4: Run Your Pi Calculator

  1. Save your Python script by clicking on “File” and then “Save”. Choose a location and name for your script, for example, pi_calculator.py
  2. Run your script by clicking on “Run” in the menu and selecting “Run Module” or press F5
  3. Follow the on-screen instructions to perform basic calculations and explore Pi-related options.

Conclusion: Embracing Math and Code on Raspberry Pi

Congratulations! You’ve successfully created a Pi Calculator, a project that not only delves into basic Python programming but also explores mathematical concepts and operations. As you continue your Raspberry Pi journey, consider enhancing your calculator with more advanced features or exploring other Python libraries for additional mathematical functionalities.

Stay tuned for more exciting Raspberry Pi projects and tutorials as you unlock the full potential of your mini-computer. Happy calculating! 🧮✨