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 andprint_receipt()prints. - A class says what every item has and can do.
Itemkeeps 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 thatprint()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 functionby_line_total()andreverse=Trueputs the most expensive lines first. - Exceptions:
add_item()raises aValueError, andtryandexceptcatch it, together with the one fromint(), 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 withtryandexcept, 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 withoutby_line_total(). - Packages. Besides the modules that come with Python, such as
mathandrandom, 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 thepipcommand, 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
In
print_receipt(), write anifstatement that printsDelivery: freewhendeliveryis0. Move the line that prints the price of the delivery into itselsebranch, so that it runs only in the other case. Our cart now getsDelivery: free(and noDelivery: $0). - 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 withTotal: $44,Delivery: $5andTo pay: $49. Then put the3back.