
成果範例
Code review: Python order-import script for a small online shop
What the code does
- Reads
orders.csvfrom the shop export. - For each order line, finds the product by SKU in
stock.xlsxand subtracts the quantity. - Writes the updated stock back and logs totals to
import_log.txt.
The structure is clear and functions are well named. Most issues are about input that does not look the way the script expects.
Bugs and weak spots
| # | Line | Severity | Issue |
|---|---|---|---|
| 1 | 48 | High | The same CSV can be imported twice; there is no check for already processed order IDs. This is the most likely cause of negative stock. |
| 2 | 72 | High | int(row["qty"]) crashes on empty or "2.0" values from refunded orders. |
| 3 | 95 | Medium | SKU lookup is case-sensitive: "gt-101" and "GT-101" are treated as different products. |
| 4 | 130 | Medium | The file is opened without encoding="utf-8", so product names with accents break on Windows. |
| 5 | 201 | Low | Bare except: hides real errors and makes the log useless. |
Corrected fragments
Issue 1: skip orders already imported
processed = load_processed_ids("processed_orders.txt")
for row in reader:
if row["order_id"] in processed:
continue
# ... existing logic ...
processed.add(row["order_id"])
save_processed_ids("processed_orders.txt", processed)
Why: each order is applied once, even if you run the script on the same file again.
Issue 2: read quantity safely
raw = (row.get("qty") or "").strip()
try:
qty = int(float(raw))
except ValueError:
log.warning("Order %s: bad quantity %r, skipped", row["order_id"], raw)
continue
Why: refunded or partial lines are skipped with a clear note instead of stopping the whole import.
Issue 3: normalise SKUs
sku = row["sku"].strip().upper()
Apply the same .strip().upper() when building the stock dictionary.
Style and readability
process()is 90 lines long; split it intoread_orders,apply_to_stock,write_report.- Replace magic column numbers (
row[7]) with header names.
Please test each fix on a copy of your files: we read the code but did not run it.
Client: owner of a tea shop who wrote a 310-line Python script that imports orders from a CSV export into the stock spreadsheet Problem reported: "Sometimes stock goes negative, and the script crashes on some files."
客戶與所有細節皆為虛構。
關於此服務
請將程式碼貼在需求說明中或附上螢幕截圖,告訴我們使用的程式語言與出了什麼問題,我們會逐行檢視。我們用白話說明程式碼的功能,指出錯誤、薄弱處與有風險的地方,並提供修正後的程式碼片段,說明每項修改的好處。我們只閱讀程式碼,不會執行,因此請務必在自己的專案中測試每項修正後再採用。
包含內容
- 您會收到訂單頁面上的書面審查:白話說明程式碼的功能、依重要性排序的問題清單、以文字呈現並附說明的修正程式碼片段,以及風格與可讀性的建議;進階版另含接下來該學習什麼的學習計畫。修正內容僅為建議,我們並未執行,請由您自行測試。
- 程式碼的功能以白話逐步說明邏輯
- 錯誤與薄弱處由嚴重到輕微排序
- 修正後的片段以文字呈現的程式碼,並說明每項修改
- 錯誤訊息您遇到的錯誤是什麼意思、從何而來
- 風格與可讀性命名、結構與重複的程式碼
- 學習計畫進階版:依序列出接下來要學的主題
服務流程
- 1需求說明您貼上程式碼並描述問題
- 2審查我們閱讀程式碼並找出問題
- 3交付您會收到審查結果與修正片段
- 4提問您測試修正,並可在方案內提出後續問題
需要您提供的資料
以文字或螢幕截圖貼上的程式碼、程式語言與版本、程式碼應有的功能、出了什麼問題,以及確切的錯誤訊息(如有)。
我們會詢問您:
- 程式碼使用什麼語言與版本?
- 程式碼應該做什麼?
- 出了什麼問題?確切的錯誤訊息是什麼?
- 您對這個程式語言有多少經驗?
- 我們應該最著重什麼?
比較方案
付款保障
下單時付款,但只有在驗收成果後,款項才會撥付。
- 固定價格付款後,方案價格不會變動。
- 款項先行保留付款時,金額只會先保留,在您驗收後才會撥付給服務。
- 交付前檢查每份成果在交到您手上之前都會經過品質檢查,您也可以在驗收前於訂單頁面查看成果。
- 免費修改在需求說明範圍內,可依方案允許的次數提出修改。
- 3 天後自動驗收若您在交付後 3 天內未驗收也未提出修改,成果將自動驗收。若您在交付後於訂單聊天室留言,訂單會暫停自動驗收;我們回覆後,可能會重新起算 3 天,並會在聊天室中告知。
- 重新製作或全額退款若成果不符合您的需求說明且修改後仍未改善,或製作時間超過交付期限的兩倍,請在訂單聊天室留言:我們將重新製作,或全額退還款項。
常見問題
你們會執行或測試我的程式碼嗎?
不會。我們閱讀並推敲程式碼,但不會執行。請在自己的專案中測試每項建議的修正。
可以開啟我的程式碼庫或網站嗎?
不行,我們不會存取程式碼庫、伺服器或帳號。請將相關程式碼貼在需求說明中,或附上最多 3 張螢幕截圖。
會幫我寫整個程式嗎?
不會。本服務是審查並說明您已有的程式碼,並提供修正後的片段。
可以幫我寫考試或需要評分的作業嗎?
不行。我們會說明您自己的程式碼,讓您能理解它。我們也不協助惡意程式、作弊或繞過授權與保護機制。
我應該先移除密碼和金鑰嗎?
應該。貼上程式碼前,請將密碼、API 金鑰和個人資料換成預留文字。
沒有找到您要的答案?