Bitpath

Boss lesson · Unit 01

A receipt

A formatted total, and your first error messages

13 exercisesabout 3 min

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?

Python
coffee = 3
cake = 4
print(coffee * 2 + cake)

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.

Python
total = 7.5
print(total)              # 7.5
print(f"{total:.2f}")     # 7.50
print(f"{2.468:.2f}")     # 2.47

Concept

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 defined

10 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.

One email when Bitpath is ready to try. Nothing else. Privacy