- 1
Write a FUNCTION(def) average that accepts a variable number of integer positional arguments and computes the average.
Write a FUNCTION(def) average that accepts a variable number of integer positional arguments and computes the average. If no arguments are supplied, the function should return None.
3 Answers
+ 1
def average(*args):
if len(args) == 0:
return None
sum = 0
for arg in args:
sum += arg
return sum / len(args)
print(average(1,2,3,4,5,6,7,8,9))
0
Thank you so much. I wanted to ask what the * sign means before args?
0
The *args parameter allows you to write a function that can accept a varying number of individual positional arguments in each call. Your code packs all of these arguments into a tuple named args . Note: The asterisk is the important part of the *args notation. The name args is just a convention that's commonly used.