The Orders

The orders have come in, and they're in a new list, orders. Each order is a record: who the customer is, which crop they want and how many pieces. Emma goes through the orders in the order they came in, and for each one:

  • when she doesn't sell the crop at all (it has no price), the customer gets nothing;
  • when there aren't enough pieces left in the barn, the customer gets nothing either: Emma doesn't sell half an order;
  • otherwise, she takes the pieces out of the barn, and the customer pays for them.

Below the barn, print a line for each order, and then how many coins Emma earned:

Total: 200 coins
Hugo: 20 potatoes for 20 coins
Anna: 4 pumpkins for 60 coins
Grandpa Joe: 12 carrots for 24 coins
Tom: not enough pumpkins
Lily: no corn at the stall
Hugo: 10 carrots for 20 coins
Earned: 124 coins

Anna has taken 4 of the 6 pumpkins, so only 2 are left when Tom wants 3. Every order changes the barn for the orders after it.

A tip: a key that isn't in a dictionary gives nil, and you know from the lesson on conditions how to check for that.

Instructions

Print what happens with each order.

Below, print how many coins Emma earned.

Start programming for free

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

Or sign up with:

2/3