Free Delivery

The shop delivers orders to the customer's door. The delivery costs $5, but orders of $50 or more are delivered for free. That's a decision, so you'll need a condition. Put it in a function that gets the total and returns the price of the delivery.

A function can have more than one return, and a condition decides which one runs. For example, a ticket for a child under 15 costs $2, and for everyone else $4:

def ticket_price(age):
    if age < 15:
        return 2
    else:
        return 4


print(ticket_price(10))
print(ticket_price(30))
2
4

Because return ends the function, you can also leave out the else and write the second return 4 below the if block, indented as much as the if. It runs only when the first one didn't.

Be careful with the condition: $50 or more means total >= 50. With >, a customer with an order of exactly $50 would still pay for the delivery.

In print_receipt(), you'll need the price of the delivery twice: on its own line, and in the amount to pay. So store it in a variable, and the function is called only once. Our cart costs $36 for now, so the customer pays for the delivery. In the next exercise, the cart will grow.

Instructions

Under cart_total(), write a function delivery_price(total) that returns 0 if total is 50 or more, and 5 otherwise.

In print_receipt(), under the line that computes total, store delivery_price(total) in a variable delivery. Under the line that prints the total, print the delivery and the amount to pay, which is the total plus the delivery: Delivery: $5 and To pay: $41.

Start programming for free

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

Or sign up with:

4/7