Answer to Question #303007 in Python for Sudheer

Question #303007

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) (Refer RollNo_W10B_1.py)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".

1
Expert's answer
2022-02-27T04:23:28-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