Congratulations!
Congratulations, your program works, and you've reached the end of the Python course! It prints a neat receipt, computes the price of every line and of the whole order, decides about the delivery, checks the quantity and merges the same items. 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.
- A list holds the cart, and
.append()adds to it. - Loops go through the cart to print it, to add up the prices and to find an item.
- Functions split the program into parts with clear jobs:
line_total()andcart_total()compute,delivery_price()decides,add_item()changes the cart andprint_receipt()prints. - Dictionaries keep together what belongs to one item: its name, its price and its quantity.
- And when something didn't work, error messages told you what went wrong and where: the last line first, then the line number.
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. - Handling errors. When the user types
abcwhere you expect a number,int()fails with aValueError. Withtryandexcept, your program can catch the error and ask again. - Files. With
open(), a program can read data from a file and save its results, so the cart doesn't disappear when the program ends. - Modules. Python comes with a lot of ready-made code:
import randomgives you random numbers (for games, for example), andimport mathgives you mathematical functions. Thousands more modules can be installed, for charts, websites, data analysis and much more. - Classes, a bit later: your own types of data, such as an
Itemwith its own functions (methods). - Your own projects. A number-guessing game, a quiz, a list of your expenses. You learn the most by solving your own problems.
And if you'd like to build websites, the HTML, CSS and JavaScript courses are waiting for you.
Instructions
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).
Check that you kept the line that prints the price of the delivery, under else: orders under $50 must still show it, for example Delivery: $5.
Start programming for free
7/7