From 5eb9c334c8917471a7dcbca95e891487c70d7cf7 Mon Sep 17 00:00:00 2001 From: Cody Hennesy Date: Wed, 7 Oct 2026 14:53:54 -0700 Subject: [PATCH] Fix calc_fine to return total fine and update docs Update the calc_fine function to return total_fine and clarify variable scopes. Fixes #196 --- episodes/writing-functions.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/episodes/writing-functions.md b/episodes/writing-functions.md index 37732c00..c562af1d 100644 --- a/episodes/writing-functions.md +++ b/episodes/writing-functions.md @@ -165,21 +165,22 @@ late_fine = 0.50 def calc_fine(days_overdue): if days_overdue <= 10: - days_overdue = days_overdue * initial_fine + total_fine = days_overdue * initial_fine else: - days_overdue = (days_overdue * initial_fine) + (days_overdue * late_fine) - return days_overdue + total_fine = (days_overdue * initial_fine) + (days_overdue * late_fine) + return total_fine ``` - `initial_fine` and `late_fine` are *global variables*. -- `days_overdue` is a *local variable* in `calc_fine`. Note that a function parameter is a variable that is automatically assigned a value when the function is called and so acts as a local variable. +- `days_overdue` and `total_fine` are *local variables* in `calc_fine`. Note that a function parameter is a variable that is automatically assigned a value when the function is called and so acts as a local variable. ```python fine = calc_fine(12) print(f'Fine owed: ${fine:.2f}') print(f'Fine rates: ${initial_fine:.2f}, ${late_fine:.2f}') print(f'Days overdue: {days_overdue}') +print(f'Total fine: {total_fine}') ``` ```output @@ -193,6 +194,7 @@ Cell In[22], line 4 2 print(f'Fine owed: ${fine:.2f}') 3 print(f'Fine rates: ${initial_fine:.2f}, ${late_fine:.2f}') ----> 4 print(f'Days overdue: {days_overdue}') + 5 print(f'Total fine: {total_fine}') NameError: name 'days_overdue' is not defined ```