Assignment 7
Assignment 7
Question one
def. calculate_budget():
Step 1: Input monthly income income =
float(input("Enter monthly income: $"))
Step 2: Fixed expenses fixed_expenses = 0.0 num_fixed =
int(input("Number of fixed expenses: ")) for i in
range(num_fixed): expense = float(input(f"Fixed expense #{i+1}:
$")) fixed_expenses += expense
Question Two.
Debugging the Logical Error
To identify and rectify the logical error causing occasional miscalculation of the remaining
budget, we can use the following debugging techniques and tools:
1. Print Statements:
Insert print statements at key points in the algorithm to display the intermediate values of
variables (e.g., income, fixed_expenses, variable_expenses, total expenses, remaining
budget).
This allows us to track the flow of the algorithm and identify any unexpected values.
2. Error Handling:
Implement error handling to catch any unexpected user inputs that may lead to incorrect
calculations.
For example, we can use try-except blocks to handle potential input errors and provide
informative error messages to the user.
3. Logging:
Utilize logging to record the values of variables and the flow of the algorithm during
execution.
This can help in identifying patterns or specific inputs that lead to the logical error.
4. Unit Testing:
Create unit tests to verify the correctness of individual components of the algorithm, such
as the calculation of total expenses and remaining budget.
This can help isolate the specific part of the algorithm causing the miscalculation.
5. Code Profiling:
Use code profiling tools to analyze the performance of the algorithm and identify any
inefficiencies or unexpected behavior.
This can reveal potential areas of the code where the logical error may be occurring.
REFERENCES.
1. Yatsko, A., & Suslow, W. (2016). Insight into theoretical and
applied informatics: Introduction to information technologies
and computer science. Walter de Gruyter GmbH.
2. https://www.python.org/about/gettingstarted/
3. https://www.freecodecamp.org/news/python-code-examples-sample-
script-coding-tutorial-for-beginners/