Write a python function that accepts a list of numbers and try to find out the value of a ‘magic index’. The ‘magic index’ is calculated as the ratio of the largest and smallest list element. The python function Magic_Index(A) Take a list of elements and return the value of the magic index or error message for any possible exceptions. The error message will be "error occurred = error name".
Example-1
Example-2
Example-3
Input:
[1, 2, 0]
Output:
Division Error: division by zero
Input:
[1, 2, 3, 6]
Output:
6.0
Input:
[1, 2, 0, "x"]
Output:
Other Error: '>' not supported between instances of 'str' and 'int'
def magic_index(arr):
try:
return max(arr)/min(arr)
except ZeroDivisionError:
return 'error occurred = ZeroDivisionError'
except TypeError:
return 'error occurred = TypeError'
except ValueError:
return 'error occurred = ValueError'
arr = [2, 3, 5.5]
print(magic_index(arr))
Comments
Leave a comment