Average of a Variable Number of Numbers

Easy ⏱ 8 min 83% acceptance ★★★★★ 4.9
Write a function average(*numbers) that returns the arithmetic mean of the given numbers as a float. If no numbers are given, return 0.0.

Examples

Example 1
Input
average(2, 4, 6)
Output
4.0
Explanation

(2+4+6)/3 = 4.0.

Example 2
Input
average()
Output
0.0
Explanation

No numbers, so the average is defined as 0.0.

Constraints

  • Any number of numeric arguments (including zero) may be passed.

Topics

Functions*args

Companies

GoogleCognizant

Hints

Hint 1

Guard against division by zero when numbers is empty.

Hint 2

sum(numbers) / len(numbers) gives the mean.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.