Files

916 B

id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
id source_exercise_id title section source_path source_repo source_commit student_visible_solution has_private_solution skip
practical-python-7.1 7.1 A simple example of variable arguments 7.1 Variable Arguments 07_Advanced_Topics/01_Variable_arguments.md https://github.com/dabeaz-course/practical-python 93dca856b41c61a0a0f85ae334116e4c125629ea false false false

Exercise 7.1: A simple example of variable arguments

Source: Practical Python Programming, 07_Advanced_Topics/01_Variable_arguments.md.

Exercise 7.1: A simple example of variable arguments

Try defining the following function:

>>> def avg(x,*more):
        return float(x+sum(more))/(1+len(more))

>>> avg(10,11)
10.5
>>> avg(3,4,5)
4.0
>>> avg(1,2,3,4,5,6)
3.5
>>>

Notice how the parameter *more collects all of the extra arguments.

关联来源