Learn
Student Becomes the Teacher
It's Okay to be Average

When teaching a class, it’s important to take the students’ averages in order to assign grades.

5 / 2 # 2 5.0 / 2 # 2.5 float(5) / 2 # 2.5

The above example is a reminder of how division works in Python.

  • When you divide an integer by another integer, the result is always an integer (rounded down, if needed).
  • When you divide a float by an integer, the result is always a float.
  • To divide two integers and end up with a float, you must first use float() to convert one of the integers to a float.

Instructions

1.

Write a function average that takes a list of numbers and returns the average.

  • Define a function called average that has one argument, numbers.
  • Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total.
  • Like the example above, use float() to convert total and store the result in total.
  • Divide total by the length of the numbers list. Use the built-in len() function to calculate that.
  • Return that result.
Folder Icon

Take this course for free

Already have an account?