4.4.3. Homework#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly 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.
### 7. Use parameters and return values
# Define `average(values)`.
# Print exactly this line:
# Average score: 86.0
scores = [92, 81, 85]
### Your code starts here.
### Your code ends here.
### 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.
# Print exactly these two lines:
# Standard: 12.5
# Custom: 13.0
price = 10.0
### Your code starts here.
### Your code ends here.
### 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.
### 10. Use recursion with a base case
# Define recursive `countdown_sum(n)`.
# Print exactly this line:
# Sum: 15
n = 5
### Your code starts here.
### Your code ends here.