Files
inf1103-labs/persistent_auditor.py
T
2026-09-25 15:09:58 +08:00

75 lines
1.8 KiB
Python

def get_valid_input():
global failed_attempts
while True:
user_input = input("Enter stock quantity: ").strip()
if user_input.lower() == "quit":
return "quit"
if not user_input.isdigit():
print("Error: Invalid input. Please enter a non-negative integer.\n")
failed_attempts += 1
continue
quantity = int(user_input)
if quantity < 0:
print("Error: Negative numbers are not allowed.\n")
failed_attempts += 1
continue
return quantity
def process_delivery(current_total, new_value):
return current_total + new_value
def calculate_tax(amount):
return amount * 0.10
def generate_report(total_units, failed_attempts):
print("\n--- Audit Report ---")
print(f"Total Units Processed: {total_units}")
print(f"Failed/Rejected Entries: {failed_attempts}")
def load_inventory():
try:
with open("inventory.txt", "r") as inventory_file:
return int(inventory_file.readline().strip())
except FileNotFoundError:
return 0
inventory = load_inventory()
total_units = 0
failed_attempts = 0
transaction_history = []
print("=== Stock Auditor ===")
print("Type 'quit' to exit.\n")
while True:
quantity = get_valid_input()
if quantity == "quit":
break
tax = calculate_tax(quantity)
transaction_history.append(quantity)
inventory = process_delivery(inventory, quantity)
total_units = process_delivery(total_units, quantity)
print(f"Stock added. Current inventory: {inventory}")
print(f"Tax on this delivery: ${tax:.2f}\n")
if inventory > 500:
print(f"ALERT: Overstock detected! Inventory ({inventory}) exceeds 500 units.")
break
generate_report(total_units, failed_attempts)