Inventory updates across a failed shipment and retry

A simplified Python 3 warehouse script processes shipment attempts. Each tuple contains an item quantity and a parcel count, both stored as strings. The last tuple retries the failed second shipment with a corrected parcel count. Converting "bad" with int raises ValueError.


Python
attempts = [("4", "2"), ("3", "bad"), ("3", "1")]
stock = 25
shipped = 0

for quantity_text, parcel_text in attempts:
    try:
        quantity = int(quantity_text)
        stock -= quantity
        parcels = int(parcel_text)
    except ValueError:
        continue
    shipped += quantity


In a revised version, move stock -= quantity immediately after parcels = int(parcel_text), keeping both statements inside try. Make no other changes. Run each version separately, starting with 25 items in stock and zero items shipped.


Find the final stock for the original version, the final stock for the revised version, and the final shipped total for each version. Give all four quantities in items as exact integers.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More try/except Blocks Questions: