Total a Sales CSV by Hand

Medium ⏱ 12 min 50% acceptance ★★★★☆ 4.3
A file holds product,amount rows (no header). Write total_sales(path) that sums the amount column as floats, skipping malformed rows (wrong field count or non-numeric amount) instead of crashing. No csv module — split on commas manually.

Examples

Example 1
Input
total_sales('sales.csv')  # 'pen,10\nbad line\nbook,25.5\n'
Output
35.5
Explanation

The malformed middle row is skipped.

Constraints

  • No csv module.
  • Malformed rows are silently skipped.

Topics

File Handlingcsv parsing

Companies

WalmartFlipkartDeloitte

Hints

Hint 1

line.split(',') should yield exactly 2 parts.

Hint 2

Wrap float() in a try/except or validate first.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.