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 ▸

Give a segment of python code that will print the numbers 1 to 7, each one on a new line


[Exam style] Python is an interpreted language. Explain what this means and how interpreted languages differ from compiled languages.


What can be used to iterate through a list in python?


How do For Loops work?