Default values
Python · Unit 4: Functions
- Full plan
- Python
- 10 exercises
A parameter can have a default value: rate=20.
If you don't pass it, it uses that value. If you pass it, it uses yours.
def tip(check, rate=20):
return check * rate / 100
print(tip(200))
print(tip(200, 25))Prints
40.0 50.0
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Predict the output
What does this code print?
3. Complete the code
Fill in the blank so the quantity is 1 when nobody says it.
4. Find the bug
This function should subtract an optional discount from the price. Which line has the error?
5. Multiple choice
Which of these definitions is valid?
6. Predict the output
What does this code print?
7. Complete the code
Fill in the blank to order the coffee with 2 sugars, without changing the milk. It should print 0 2.
8. Put the lines in order
Put in order the function that repeats an alert. It should print Goal! twice.
9. Find the case that fails
split(total, people)splits a check. If nobody says how many people there are, one person pays it all. Which call makes it fail?10. Predict the output
What does this code print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.