Write a function that takes a list of numbers as input, and outputs the average of the numbers. The function should catch any errors.

def average(numberList):  try:    total=0    length=len(numberList) #len finds the length of a list    for i in range(length): #adds up the numbers      total=total+numberList[i]    return total/length #returns the final total  except: #is only executed if an error occurs    return "error"

JP

Related Python Mentoring answers

All answers ▸

Explain the difference between local and global variables


Ask the user for a number and output the smallest divisor, bigger than one, for the inputted number. Output "Prime" if the number is a prime number.


Whats the output of "for i in "hello world": print(i)" and why/how is this achieved


What is the difference between a FOR loop and WHILE loop and how do I write them?