Ask Question
31 August, 14:36

Description: Write a function that takes in a list of numbers and a list of indices. Note that indexList may not only contain valid indices. The function should keep track of the number and type of errors that occur. Specifically, it should account for IndexError and TypeError. It should return the average of all the numbers at valid indices and a dictionary containing the number and type of errors together in a tuple. errorDict should be formatted as follow

+1
Answers (1)
  1. 31 August, 14:50
    0
    Python code is explained below

    Explanation:

    average, count, indexerror, typeerror variables are initialised to 0

    Then, for loop is used to traverse the indexlist, if type is not right, typeerror is incremented, else if index is not right, indexerror is incremented, otherwise, count is incremented, and the number is added to average.

    At last, average variable which contains the sum of numbers is divided by count to get average.

    Here is the code:

    def error_finder (numList, indexList):

    average = 0

    count = 0

    indexerror = 0

    typeerror = 0

    for i in range (len (indexList)):

    if type (indexList[i]) = =int:

    if indexList[i]>=len (numList) or i<0:

    indexerror = indexerror + 1

    else:

    average = average + numList[indexList[i]]

    count = count+1

    else:

    typeerror = typeerror + 1

    d = {"IndexError": indexerror, "TypeError":typeerror}

    average = average/count

    return (average, d)

    print (error_finder ([4, 5, 1, 7, 2, 3, 6], [0, "4", (1,), 18, "", 3, 5.0, 7.0, {}, 20]))
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Description: Write a function that takes in a list of numbers and a list of indices. Note that indexList may not only contain valid ...” in 📗 Engineering if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers