Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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. The exercise tries your program with an order of exactly $50 too.

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

  1. 1

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

  2. 2

    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.

Back

4/9

  1. 1. The Shopping Cart
  2. 2. The Price of a Line
  3. 3. The Total
  4. 4. Free Delivery
  5. 5. Adding Items
  6. 6. The Same Item Twice
  7. 7. Reading the Order
  8. 8. The Most Expensive First
  9. 9. Congratulations!
Next

Try it yourself

Do this exercise right here in your browser. No account needed.