Homework

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.

Hide code cell source

### Solution
# True
# Defining the fee calculation once in a function lets every report call it by name. If
# the fee rule changes, you update one function instead of hunting down every copied
# expression.

2. Return Values

A function that only prints a value automatically returns that printed text to the caller.

Hide code cell source

### Solution
# False
# print() only displays a value; it does not send anything back to the caller. A function
# without a return statement returns None, so you need return to give the caller a value.

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.

Hide code cell source

### Solution
# True
# A name assigned inside a function exists only in that function's local scope and
# disappears when the call ends. A return statement sends back the value, which the caller
# can store under its own name, but the local name is still not visible outside the
# function.

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.

Hide code cell source

### Solution
# True
# A default parameter such as tax_rate=0.08 supplies the common value when the caller
# omits the argument. The few calls that need a different rate can still pass their own
# value.

5. Base Cases

A recursive function can omit a base case as long as the recursive call moves toward smaller values.

Hide code cell source

### Solution
# False
# Every recursive function needs a base case that returns without making another recursive
# call. Moving toward smaller values only helps if some base case stops the recursion;
# without one, Python keeps calling until it raises RecursionError.
### 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.

Hide code cell source

### Solution
def format_total(amount):
    return f"Total due: {amount:.2f}"

amount = 42.75
print(format_total(amount))
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.

Hide code cell source

### Solution
def average(values):
    return sum(values) / len(values)

scores = [92, 81, 85]
print("Average score:", round(average(scores), 1))
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.

Hide code cell source

### Solution
def add_fee(price, fee=2.5):
    return price + fee

price = 10.0
print("Standard:", add_fee(price))
print("Custom:", add_fee(price, fee=3.0))
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.

Hide code cell source

### Solution
def subtotal(values):
    return sum(values)

def with_tax(amount, tax_rate):
    return round(amount * (1 + tax_rate), 2)

prices = [12, 8, 10]
tax_rate = 0.08
print("Total:", with_tax(subtotal(prices), tax_rate))
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.

Hide code cell source

### Solution
def countdown_sum(n):
    if n == 0:
        return 0
    return n + countdown_sum(n - 1)

n = 5
print("Sum:", countdown_sum(n))
Sum: 15