Boss lesson · Unit 01
A receipt
A formatted total, and your first error messages
What you practise
- Prices, quantities, a discount and tax turned into a formatted total, one line at a time
- Reading the first error messages (
NameError,TypeError,SyntaxError)
Try a question from this lesson
Pick one
A cafe wants its till to print receipts. Start small: two coffees at 3 each and one cake at 4. What does this show?
coffee = 3
cake = 4
print(coffee * 2 + cake)Correct answer
10
Why
Multiplying comes first: 3 * 2 is 6 for the two coffees, then 6 + 4 is 10. Only the coffee is doubled. Doubling the cake as well would give 14.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Showing money
Money needs exactly two decimal places: 7.50, not 7.5.
Inside the curly brackets of an f-string, add :.2f after the value. It means "show this as a float with 2 digits after the point", rounding if it has to.
total = 7.5
print(total) # 7.5
print(f"{total:.2f}") # 7.50
print(f"{2.468:.2f}") # 2.47Concept
Reading an error
When Python stops, it prints a report. Read the last line first: it names the kind of error and says what went wrong.
The lines above it say where: the file, the line number, and the line itself with marks under the problem.
A NameError is a name Python has not been given: a typo, or a line that ran too early.
Traceback (most recent call last):
File "/app/pay.py", line 2, in <module>
total = price + fee
^^^
NameError: name 'fee' is not defined10 more exercises in the app
- 3× type the answer
- 2× fill the blanks
- 3× multiple choice
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.