top of page

25% Discount For All Pricing Plans "welcome"

Advanced Calculator

What is an Advanced Calculator?


An Advanced Calculator is an application that allows users to perform various mathematical operations. In addition to basic arithmetic operations, it supports more advanced functions like square root, exponentiation, and trigonometry. Such applications are used to enhance calculation capabilities and develop mathematical skills.


Project Objectives and Student Benefits

Through this project, students will reinforce their ability to develop simple and functional programs using basic Python knowledge. Additionally, they will gain experience in user interaction and mathematical calculations.


Project Outputs

  • Create a calculator application using basic Python structures and control flows.

  • Collect data from users and manage calculation operations by processing this data.

  • Write more modular and readable code using functions.


Step-by-Step Tasks of the Project

  1. Collect the operation type and necessary data from the user.

  2. Perform the operation and calculate the result.

  3. Print the result.

  4. Make the code more modular using functions.


Solution Without Functions


Step 1: Collect the Operation Type and Necessary Data from the User

In the first step, the operation type and necessary data will be collected from the user.

# Collecting the operation type and necessary data from the user
print("Select the operation type:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Square Root")
print("6. Exponentiation")
print("7. Sine")
print("8. Cosine")
print("9. Tangent")

operation_type = int(input("Enter the operation type (1-9): "))

if operation_type in [1, 2, 3, 4, 6]:
    num1 = float(input("Enter the first number: "))
    num2 = float(input("Enter the second number: "))
elif operation_type == 5:
    num1 = float(input("Enter the number: "))
elif operation_type in [7, 8, 9]:
    import math
    degrees = float(input("Enter the degrees: "))
    radians = math.radians(degrees)

This code snippet collects the operation type and necessary data from the user.


Step 2: Perform the Operation and Calculate the Result

In the second step, the selected operation is performed, and the result is calculated.

# Performing the operation and calculating the result
if operation_type == 1:
    result = num1 + num2
elif operation_type == 2:
    result = num1 - num2
elif operation_type == 3:
    result = num1 * num2
elif operation_type == 4:
    if num2 != 0:
        result = num1 / num2
    else:
        result = "Undefined (division by zero)"
elif operation_type == 5:
    result = math.sqrt(num1)
elif operation_type == 6:
    result = num1 ** num2
elif operation_type == 7:
    result = math.sin(radians)
elif operation_type == 8:
    result = math.cos(radians)
elif operation_type == 9:
    result = math.tan(radians)

This code snippet performs the selected operation and calculates the result.


Step 3: Print the Result

In the third step, the calculated result is printed.

# Printing the result
print("Result:", result)

This code snippet prints the calculated result.


Solution with Functions


Step 1: Function to Collect the Operation Type and Necessary Data from the User

In the first step, the operation type and necessary data will be collected from the user.

def collect_data(operation_type):
    """Collects necessary data from the user."""
    if operation_type in [1, 2, 3, 4, 6]:
        num1 = float(input("Enter the first number: "))
        num2 = float(input("Enter the second number: "))
        return num1, num2
    elif operation_type == 5:
        num1 = float(input("Enter the number: "))
        return num1, None
    elif operation_type in [7, 8, 9]:
        import math
        degrees = float(input("Enter the degrees: "))
        radians = math.radians(degrees)
        return radians, None

This function collects the necessary data from the user and returns it.


Step 2: Function to Perform the Operation and Calculate the Result

In the second step, the selected operation is performed, and the result is calculated.

def calculate(operation_type, num1, num2=None):
    """Performs the operation and calculates the result."""
    import math
    if operation_type == 1:
        return num1 + num2
    elif operation_type == 2:
        return num1 - num2
    elif operation_type == 3:
        return num1 * num2
    elif operation_type == 4:
        if num2 != 0:
            return num1 / num2
        else:
            return "Undefined (division by zero)"
    elif operation_type == 5:
        return math.sqrt(num1)
    elif operation_type == 6:
        return num1 ** num2
    elif operation_type == 7:
        return math.sin(num1)
    elif operation_type == 8:
        return math.cos(num1)
    elif operation_type == 9:
        return math.tan(num1)

This function performs the selected operation and calculates the result.


Step 3: Function to Print the Result

In the third step, the calculated result is printed.

def print_result(result):
    """Prints the calculated result."""
    print("Result:", result)

This function prints the calculated result.


Step 4: Function to Start the Calculator Application

This function starts the Advanced Calculator application and manages the application's flow.

def start_calculator():
    """Starts the Advanced Calculator application."""
    print("Select the operation type:")
    print("1. Addition")
    print("2. Subtraction")
    print("3. Multiplication")
    print("4. Division")
    print("5. Square Root")
    print("6. Exponentiation")
    print("7. Sine")
    print("8. Cosine")
    print("9. Tangent")

    operation_type = int(input("Enter the operation type (1-9): "))
    num1, num2 = collect_data(operation_type)
    result = calculate(operation_type, num1, num2)
    print_result(result)

# Starting the application
start_calculator()

This function starts the Advanced Calculator application and manages its flow. It collects the operation type and necessary data from the user, performs the operation, and prints the result.


Concepts Used

  • Data Types: In this project, numbers (float, int) and strings (user input) are used.

  • Conditional Statements: if-else structures are used to determine the operation type and perform the correct operation.

  • Functions: Functions make the code modular and reusable.

  • Mathematical Calculations: The math module is used to perform square root, exponentiation, and trigonometry operations.

Comments


bottom of page