Kodewick
Start learning →
Mini challenge←→0 / 80

Pocket ledger

#054Mini challengeErrorsStrings7 min

You keep a money log in your phone notes, one line each like "coffee 12", amounts in whole numbers. Some lines are broken: no amount, an extra word, or an amount that isn't a number. Write ledger_total(lines) to add up the good lines and skip the broken ones.

Examples
ledger_total(["coffee 12", "bus 3"])→15
ledger_total(["lunch", "tea abc", "snack 5"])→5
main.py
def ledger_total(lines):
    total = 0
    for line in lines:
        # your code here: split the line in two, turn the amount into an int and add it to total; skip it on ValueError
        pass
    return total
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.