PrashanthB461's picture
Update calculator/operations.py
1f5aef9 verified
raw
history blame contribute delete
946 Bytes
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!"