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
Answered by Jonathan P. Python tutor

1681 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

When do we use a for-loop and when a while-loop?


How can you take a list of numbers and add 3 to every number in the list using only one line of code?


Create a python code to sum the number from 1 to 10.


What is the difference between a cycle "if" and "while"?


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences