G-XR8P2XJ088

AI-Written Python: Keep Shortages in Your Stock Check

A print shop expects twelve fern prints and eight moon prints on the shelf. The team counts nine fern prints and ten moon prints. An AI-written Python script compares the counts and reports two extra moon prints. It says nothing about the missing fern prints. Nothing crashed. The script used subtraction that keeps only positive results.

That can be a useful rule when you want a list of surpluses. For a stock check, it leaves out the half of the result you may need first. The important choice happens before the code: are you asking for units that exceed the expected count, or for every difference between the shelf and the record?

Two kinds of subtraction answer different questions

Python’s documentation for collections.Counter explains that a Counter can store positive, zero and negative counts. But the minus operator between two Counters creates a result containing only entries with a positive count. If you write counted - expected, the two extra moon prints remain and the three missing fern prints disappear. Swapping the operands gives you a list of shortages expressed as positive quantities, but now the surpluses disappear.

The subtract() method keeps signed results and updates the Counter it is called on. Make a separate working Counter first, for example with difference = counted.copy(), then call difference.subtract(expected). With the rule ‘counted minus expected’, the result is minus three for fern and plus two for moon. Keep the original counts for comparison. If the report only needs discrepancies, filter out exact zeroes after the calculation and preserve both positive and negative values.

Either approach can be appropriate. If a workflow specifically needs a picking list for replacement stock, a shortage-only result may be convenient. If people use the report to decide whether to investigate misplaced stock, fulfil orders or correct a record, they need to know which direction each difference goes. Use headings such as ‘counted’, ‘expected’ and ‘counted minus expected’ so a colleague does not have to infer the sign convention.

A blank record needs its own rule

A Counter returns zero when you look up a key it does not contain. That is convenient when absence genuinely means no units. It is a poor substitute for checking whether a count took place. In the print shop, a missing fern row might mean someone confirmed the shelf was empty. It could also mean the storeroom was locked or the wrong worksheet was imported. Those cases should lead to different actions.

If your count sheet can distinguish ‘counted zero’ from ‘not checked’, preserve that information separately. For an incomplete count, show the missing observation and leave the variance unresolved until someone checks it. If you are comparing units across multiple locations, agree whether the report should show each location separately or combine them. A surplus in the packing room can cancel a shortage at the front desk while leaving both teams with a problem.

Also make the unit of comparison explicit. A bundle of six postcards and an individual postcard are different units unless you have agreed a conversion. Our article on counting expanded order rows in pandas shows another way a report can change what one record represents. Here, even correct subtraction will mislead if the expected and counted columns describe different units or different locations.

Choose a shared counting cut-off as well. If the expected file was exported before the afternoon dispatch but the shelf was counted afterwards, the missing prints may already be on their way to customers. Keep the two timestamps with the report and identify movements between them. That gives the person investigating a way to distinguish a timing mismatch from a discrepancy that needs a recount.

Make the generated code fail a useful test

Before running the script on the real stock file, give it a tiny example with the results written down by hand. Do not test only cases in which counted stock exceeds expectations. Include one case that makes a positive-only result look reassuring even though there is a shortage.

  • One surplus and one shortage: The moon print shows +2 and the fern print shows −3. Both must be visible in a discrepancy report.
  • An exact match: A third product has five expected and five counted. It may be omitted from a discrepancy view, but the underlying count should remain available.
  • A confirmed zero and an unchecked shelf: A checked empty shelf can yield a shortage. An unchecked shelf should be marked incomplete rather than treated as empty.
  • Two locations: The same product is short in one place and over in another. If location matters, both differences should be visible even when the combined difference is zero.

Ask for a report that people can reconcile, with the source counts beside the differences. It can show shortages and surpluses in separate sections without losing the signed figures underneath. A net total by itself can also hide the work: in the example, the combined difference is only one unit short, but there are two products to investigate. Keep the product-level differences even when you show a summary.

Finally, keep reporting separate from correction. A signed difference tells you what the count and the system disagree on. It does not tell you which record should change or whether an order in transit explains the gap. Start by putting both sides of the discrepancy in front of the person who can check them. That gives the report a clear job and keeps a convenient Python shortcut from deciding what the business gets to see.

Source

  • Python 3.13 Library Reference, collections.Counter, covering missing keys, signed counts, subtract() and multiset arithmetic.

Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse

Subscribe to get the latest posts sent to your email.

One thought on “AI-Written Python: Keep Shortages in Your Stock Check”

Leave a Reply

Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse

Subscribe now to keep reading and get access to the full archive.

Continue reading