runtime
176 / 500
question 176 Exceptions & Validation · medium

Every problem is "unexpected"

load_prices should say *why* each bad row was skipped, but the report says unexpected problem for every one of them, so nobody can fix the feed. Fix the handlers so missing columns and bad numbers get their own messages, while anything else is still reported as unexpected.

Functionload_prices(rows)
Returns(prices, problems): {stock_code: float price} for good rows, and a list of "row <n>: …" texts numbered from 1
Messagesmissing <column> for a KeyError, bad number <message> for a ValueError, unexpected problem for anything else
Keepthe function name, the messages and what it returns; only the bug should change
Examples
callload_prices([{'StockCode': '85048', 'Price': '6.95'}, {'StockCode': '22350', 'Price': '2,55'}, {'StockCode': '22087'}])returns({'85048': 6.95}, ["row 2: bad number could not convert string to float: '2,55'", 'row 3: missing Price'])
callload_prices([{'StockCode': '21329', 'Price': '0.98'}])returns({'21329': 0.98}, [])
callload_prices([])returns({}, [])

Loading your progress…

Every problem is "unexpected" — Python practice · Runtime