#149EasyErrors & Exceptions· try / except
Safe Integer Parser
Input format
`items`: a list of strings. Test data only uses ASCII characters and no underscores.
Output format
A dictionary with keys `total` and `errors`.
Examples
Input: items = ["10"," -3 ","abc","4.5"]
Output: {"total":7,"errors":["item 3: \"abc\" is not a whole number","item 4: \"4.5\" is not a whole number"]}
10 and -3 convert. `abc` and `4.5` raise `ValueError` in `int()`.
Input: items = []
Output: {"total":0,"errors":[]}
Nothing to add and nothing to report.
Constraints
- 0 <= len(items) <= 10^4
Study first
- #python
- #exceptions
- #try-except
- #validation
