Kodewick
Start learning →
Mini challenge←→0 / 80

Shopping note

#029Mini challengeStringsLoops8 min

The note on the fridge says "milk x2, bread, eggs x12": things are split by commas, and the number after x says how many; with no number it's 1. Write count_items(note) to return how many things to buy in all. An empty note gives 0.

Examples
count_items("milk x2, bread, eggs x12")→15
count_items("")→0
main.py
def count_items(note):
    if note.strip() == "":
        return 0
    total = 0
    for part in note.split(","):
        part = part.strip()
        # your code here: find " x"; if it's there add the number after it, otherwise add 1
        pass
    return total
Ctrl + Enter runs too
Result

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