4.4.3. Homework#
Use these questions after class to check your understanding of the chapter concepts and coding skills.
Sign in from the account menu before you start working on the homework.
Complete all true/false and coding questions before you submit the homework.
Launch Live Code (the rocket) to write and run code in the coding cells.
Multiple submissions are allowed. The highest score is kept.
1. Function Calls
A team that calculates the same fee in several reports should put that calculation in a function instead of copying the same expression repeatedly.
2. Return Values
A function that only prints a value automatically returns that printed text to the caller.
3. Scope
A variable created inside a function is local to that function. Returning its value lets the caller use the value, but the name itself stays local.
4. Default Parameters
A reporting helper can use a default parameter when most calls use the same tax rate but a few calls need a different rate.
5. Base Cases
A recursive function can omit a base case as long as the recursive call moves toward smaller values.
### 6. Write a small formatter function
# Define `format_total(amount)` so the code prints exactly this line:
# # Total due: 42.75
amount = 42.75
### Your code starts here.
### Your code ends here.
Total due: 42.75
### 7. Use parameters and return values
# Define `average(values)` and print exactly this line:
# # Average score: 86.0
scores = [92, 81, 85]
### Your code starts here.
### Your code ends here.
Average score: 86.0
### 8. Use a default argument
# Define `add_fee(price, fee=2.5)` that returns price plus fee.
# Call it once with the default fee, then once with a custom fee of 3.0, and print exactly these two lines:
# # Standard: 12.5
# # Custom: 13.0
price = 10.0
### Your code starts here.
### Your code ends here.
Standard: 12.5
Custom: 13.0
### 9. Compose small functions
# Define `subtotal` and `with_tax` helpers. Print exactly this line:
# # Total: 32.4
prices = [12, 8, 10]
tax_rate = 0.08
### Your code starts here.
### Your code ends here.
Total: 32.4
### 10. Use recursion with a base case
# Define recursive `countdown_sum(n)` and print exactly this line:
# # Sum: 15
n = 5
### Your code starts here.
### Your code ends here.
Sum: 15