From 6eda77cc8821bd769423b9f589395b681e327713 Mon Sep 17 00:00:00 2001 From: Wind-Explorer Date: Fri, 25 Sep 2026 15:10:53 +0800 Subject: [PATCH] save persistent inventory --- persistent_auditor.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/persistent_auditor.py b/persistent_auditor.py index cb8f6ce..c25e082 100644 --- a/persistent_auditor.py +++ b/persistent_auditor.py @@ -39,15 +39,24 @@ def generate_report(total_units, failed_attempts): def load_inventory(): try: with open("inventory.txt", "r") as inventory_file: - return int(inventory_file.readline().strip()) + inventory = int(inventory_file.readline().strip()) + history_line = inventory_file.readline().strip() + history = [] if not history_line else [int(value) for value in history_line.split(",")] + return inventory, history except FileNotFoundError: - return 0 + return 0, [] -inventory = load_inventory() +def save_inventory(inventory, transaction_history): + with open("inventory.txt", "w") as inventory_file: + inventory_file.write(f"{inventory}\n") + inventory_file.write(",".join(str(quantity) for quantity in transaction_history)) + inventory_file.write("\n") + + +inventory, transaction_history = load_inventory() total_units = 0 failed_attempts = 0 -transaction_history = [] print("=== Stock Auditor ===") print("Type 'quit' to exit.\n") @@ -56,6 +65,7 @@ while True: quantity = get_valid_input() if quantity == "quit": + save_inventory(inventory, transaction_history) break tax = calculate_tax(quantity)