Workshop 6 introduces modules, imports, and simple composition patterns. Complete the tasks in a new file (for example, homework_6.py) so you can run everything together.
- Create a module-level function
square(number)that returns the number squared. - Create another function
shift_and_square(number, offset)that adds the offset to the number, then callssquareand returns the result. - Demonstrate both functions by printing at least two sample calculations.
- Reuse the idea from
workshop_6/composition.pyto model a simple payment attempt. - Implement
check_balance()that randomly returnsTrueorFalse(userandom.choice). - Implement
complete_transaction()that prints a confirmation message and returns a dictionary describing the order. - Implement
place_order()that only callscomplete_transaction()whencheck_balance()returnsTrue; otherwise, it returns a dictionary with a failed status. Include anorder_idandtotalin both success and failure paths (useNonefor failed orders if needed). - Run the
place_order()function three times, collect the results in a list, and print a human-readable summary for each attempt.
- Create a separate file named
helpers.pythat contains thesquareandshift_and_squarefunctions from Task 1. - In
homework_6.py, importhelpersand demonstrate the functions via the module namespace (helpers.square,helpers.shift_and_square). - Explain in a short comment why keeping helper functions in a separate module can make larger projects easier to manage.
- Write a function
safe_place_order(max_attempts=3)that tries to callplace_order()until it receives a success or exhaustsmax_attempts. - Track and print each attempt number and outcome.
- Return the successful order or
Noneif every attempt fails.
- Extend
complete_transaction()to accept**kwargsfor additional metadata (for example,coupon="SAVE10"). - Add a simple logger function that timestamps each step using
datetimeand prints a consistent prefix. - Write a mini-test section that imports
helpersand asserts the output ofsquare()andshift_and_square()for a few inputs.