Answer to Question #302246 in Python for Michael

Question #302246

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'



1
Expert's answer
2022-02-24T12:18:55-0500
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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS