8.6. A function that accumulates¶
We have used the
len function a lot already. If it weren’t part of Python, our lives as programmers would have been
a lot harder.
Well, actually, not that much harder. Now that we know how to define functions, we could define
len ourselves if it
did not exist. Previously, we have used the accumlator pattern to count the number of lines in a file. Let’s use that
same idea and just wrap it in a function definition. We’ll call it
mylen to distinguish it from the real
which already exists. We actually could call it len, but that wouldn’t be a very good idea, because it would replace
the original len function, and our implementation may not be a very good one.
Rearrange the code statements to match the activecode window above. (This is an exercise in noticing where the indenting and outdenting happens, and where the return statement goes.)
Check your Understanding
1. Write a function named
total that takes a list of integers as input, and returns the total value of all those integers added together.
2. Write a function called
count that takes a list of numbers as input and returns a count of the number of elements in the list.