December 2017
Beginner
412 pages
10h 8m
English
You’ll often write a function that works only in some situations. For example, you might write a function that takes as a parameter a number of people who want to eat a pie and returns the percentage of the pie that each person gets to eat. If there are five people, each person gets 20% of the pie; if there are two people, each person gets 50%; if there is one person, that person gets 100%; but if there are zero people, what should the answer be?
Here is an implementation of this function:
| | def pie_percent(n: int) -> int: |
| | """Assuming there are n people who want to eat a pie, return the |
| | percentage of the pie that each person gets to eat. |
| | |
| | >>> pie_percent(5) |
| | 20 |
| | >>> ... |
Read now
Unlock full access