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

Congratulations!

Congratulations, your program works, and you've reached the end of the Python course! It reads the order from the website, skips the lines that make no sense, merges the same items and prints a neat receipt, with the most expensive lines first, the total and the delivery. And you wrote all of it yourself.

One last improvement. The receipt says Delivery: $0. That's correct, but shops write free, which sounds much better. In print_receipt(), print Delivery: free when the delivery costs nothing, and the price as before otherwise.

What you've learned. Look at how each part of the course found its place in your program:

  • Variables and f-strings hold the total and the delivery, and put together the lines of the receipt.
  • Conditions decide about the delivery, check the quantity and find the same item.
  • Lists hold the cart and the order, and .append() adds to the cart.
  • Loops go through the order line by line, and through the cart to print it and to find an item.
  • Functions split the program into parts with clear jobs: cart_total() computes, delivery_price() decides, add_item() changes the cart and print_receipt() prints.
  • A class says what every item has and can do. Item keeps its name, price and quantity together, as a dictionary could, and adds methods: line_total() computes the price of its line, and __str__ gives the line that print() shows.
  • Text methods cut the lines from the website into parts with .split(), and .strip() with .lower() find the milk however the customer typed it. Unpacking puts the parts of a line into three variables at once.
  • A list comprehension collects the prices of the lines for sum().
  • Sorting with sorted(), your key function by_line_total() and reverse=True puts the most expensive lines first.
  • Exceptions: add_item() raises a ValueError, and try and except catch it, together with the one from int(), so that one bad line doesn't stop the order.
  • And when something didn't work, tracebacks told you what went wrong and where: from the bottom, the error, then the line and the function where it happened.

A program uses what it needs, so not all of the course is on this receipt. Modules would come in if the shop added a small gift to some orders, picked with random.choice(). And inheritance would, if the shop sold some goods by weight: a class WeighedItem(Item) would get everything from Item and replace only line_total().

Python on your own computer. The programs here run in your browser. The easiest way to run them on your own computer is Thonny (thonny.org), a free editor made for beginners, with Python built in. Install it, paste your program into it, save it as receipt.py and run it with the green Run button (or the F5 key). Later, you can install Python from python.org and write your programs in a code editor such as Visual Studio Code.

What's next. You now know the basics that every Python program is built from. Here are some ideas for what to learn next:

  • Input from the user. On your computer, input() works: name = input("What would you like to buy? ") waits until the user types something and presses Enter, and gives you the text. (Here in the browser, there's no one to type, so it doesn't work.) Your shop could ask what to put in the cart, and with try and except, ask again when the quantity isn't a number.
  • Files. With open(), a program can read data from a file and save its results. The order could come from a file that the website saves, and the receipt could go into another one.
  • lambda. A small function without a name, written right where it's needed: sorted(items, key=lambda item: item.line_total(), reverse=True) sorts the receipt without by_line_total().
  • Packages. Besides the modules that come with Python, such as math and random, there are hundreds of thousands of packages written by other programmers on PyPI (pypi.org): for charts, games, websites, data analysis and much more. You install them with the pip command, or in Thonny under Tools, Manage packages.
  • Your own projects. A number-guessing game, a quiz, a list of your expenses. You learn the most by solving your own problems.

And there are more courses waiting for you: SQL to keep a shop's data in a database, and HTML, CSS and JavaScript to build websites.

Instructions

  1. 1

    In print_receipt(), write an if statement that prints Delivery: free when delivery is 0. Move the line that prints the price of the delivery into its else branch, so that it runs only in the other case. Our cart now gets Delivery: free (and no Delivery: $0).

  2. 2

    Check that smaller orders still show the price of the delivery. In the cart, change the chocolate to one bar, Item("Chocolate", 5, 1), and run the program: the receipt ends with Total: $44, Delivery: $5 and To pay: $49. Then put the 3 back.

Back

9/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!

Try it yourself

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