Kodewick
Start learning →
Starter←→0 / 80

Pet weigh-in

#070StarterStringsDicts3 min

The vet's notes have one pet per line: a name and a weight (whole kilos), with one space between. Write read_weights(text) to make a {name: weight} dict, with the weights as numbers. If a pet was weighed twice, keep the later one.

Examples
read_weights("Momo 4\nRex 12")→{'Momo': 4, 'Rex': 12}
read_weights("")→{}
main.py
def read_weights(text):
    weights = {}
    for line in text.splitlines():
        name, kg = line.split()
        # your code here: turn the weight into a number and store it in the dict
    return weights
Ctrl + Enter runs too
Result

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