File size: 946 Bytes
bda2e64
1f5aef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bda2e64
1f5aef9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def perform_calculation(numbers, operation):
    try:
        # Handle operations based on the operation type
        if operation == "add":
            return sum(numbers)
        elif operation == "subtract":
            return numbers[0] - sum(numbers[1:])
        elif operation == "multiply":
            return multiply(numbers)
        elif operation == "divide":
            return divide(numbers)
        else:
            return "Error: Unsupported operation!"
    except Exception as e:
        return f"Error: {str(e)}"

# Helper function for multiplication
def multiply(numbers):
    result = 1
    for num in numbers:
        result *= num
    return result

# Helper function for division with ZeroDivisionError handling
def divide(numbers):
    try:
        result = numbers[0]
        for num in numbers[1:]:
            result /= num
        return result
    except ZeroDivisionError:
        return "Error: Division by zero!"