What's in the Barn

Time for one more project! This checkpoint puts the functions and dictionaries from the last two lessons to work, together with everything you learned before them. As in the previous checkpoints, the instructions say what the program should print, how you write it is up to you, and each exercise also runs your program with other data.

This time, keep one more thing in mind: write the parts of the program as functions. The exercises don't check that, but each step builds on the previous one, and in the last one, you'll need some of the earlier work again.

The project. On Saturdays, Emma sells her crops at her farm stall, and customers order them in advance. Over three exercises, your program will work out what the crops in the barn are worth, handle the day's orders and, in the evening, sum up the day.

The data. Two dictionaries hold what you need. barn says how many pieces of each crop are in the barn, and prices how many coins one piece of each crop sells for. Both have a key for every crop: barn.pumpkins is 6, and prices.pumpkins is 15. Each pair is on a line of its own, so that you can easily change it or add another crop, and the exercise does that too.

The task. Print every crop with its number of pieces, its price and what all its pieces are worth, starting with the crop that's worth the most. When two crops are worth the same, the one that comes first in the alphabet goes first. Below the crops, print what everything in the barn is worth together:

pumpkins: 6 x 15 = 90 coins
carrots: 30 x 2 = 60 coins
potatoes: 50 x 1 = 50 coins
Total: 200 coins

Remember that pairs goes through the keys in no particular order, so the program has to put them in order itself.

A tip: in the lesson on dictionaries, you collected the keys into a list to sort them. And in the lesson on functions, you sorted with a comparison function that had a second rule for items that are equal by the first one.

Instructions

Print every crop in the barn, starting with the one that's worth the most.

Below, print what all the crops are worth together.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/3