- 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.

22nd Oct 2017, 4:00 PM
Бауыржан
Бауыржан - avatar
3 Réponses
+ 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))
22nd Oct 2017, 4:23 PM
ChaoticDawg
ChaoticDawg - avatar
0
Thank you so much. I wanted to ask what the * sign means before args?
23rd Oct 2017, 6:11 AM
Бауыржан
Бауыржан - avatar
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.
22nd Aug 2024, 2:25 PM
GAMING JOHAN
GAMING JOHAN - avatar