Files
coding-mentor-agent/kb/python-course-kb-practical-python/wiki/exercises/2-15-a-practical-enumerate-example.md
T

1.2 KiB
Raw Blame History

id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
id source_exercise_id title section source_path source_repo source_commit student_visible_solution has_private_solution skip
practical-python-2.15 2.15 A practical enumerate() example 2.4 Sequences 02_Working_with_data/04_Sequences.md https://github.com/dabeaz-course/practical-python 93dca856b41c61a0a0f85ae334116e4c125629ea false false false

Exercise 2.15: A practical enumerate() example

Source: Practical Python Programming, 02_Working_with_data/04_Sequences.md.

Exercise 2.15: A practical enumerate() example

Recall that the file Data/missing.csv contains data for a stock portfolio, but has some rows with missing data. Using enumerate(), modify your pcost.py program so that it prints a line number with the warning message when it encounters bad input.

>>> cost = portfolio_cost('Data/missing.csv')
Row 4: Couldn't convert: ['MSFT', '', '51.23']
Row 7: Couldn't convert: ['IBM', '', '70.44']
>>>

To do this, you’ll need to change a few parts of your code.

...
for rowno, row in enumerate(rows, start=1):
    try:
        ...
    except ValueError:
        print(f'Row {rowno}: Bad row: {row}')

关联来源