Start learning free

LESSON 186 OF 673 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in Python: Single Item Total

Practice Read the full explanation

Practice variables, arithmetic, and string concatenation by building a tiny one-item receipt.

You start with:

item = "Coffee Mug"
price = 12
quantity = 3

Build:

Coffee Mug x3
Total: $36

Rules:

  • Compute total = price * quantity.
  • First line: "<item> x<quantity>" (e.g. "Coffee Mug x3").
  • Second line: "Total: $<total>".
  • Join the two lines with a newline "\n" into one string result. Just store the text in result; you do not need to print it.

Wrap each integer with str(...) before concatenating it with +.

line1 + "\n" + line2 joins both lines into one string.

item = "Coffee Mug"
price = 12
quantity = 3

total = price * quantity
line1 = item + " x" + str(quantity)
line2 = "Total: $" + str(total)
result = line1 + "\n" + line2

STEP 1 OF 4
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in Python, explained

    Problem overview

    This is the first version of a shopping cart program. It builds a tiny receipt for one item. The starting variables are item, price and quantity. The program calculates the total cost and stores a two-line receipt in a string called result. It does not print anything: the text only has to be stored.

    With a coffee mug that costs 12, bought three times, the total is 36 and the receipt is:

    Coffee Mug x3
    Total: $36

    Approach: calculate, then build each line

    The work splits into arithmetic and text. The arithmetic is one multiplication: the price of one mug repeated once per mug. The text part builds two rows and joins them with a line break.

    As in other string-building lessons, each row gets its own variable, line1 and line2. That keeps every expression short and lets you check each row on its own. The text rows contain numbers, and Python's + operator joins only strings, so each number has to be converted with str() first.

    Step by step

    1. Compute total. Write price * quantity. Both are integers, so the result is an integer: 36.
    2. Build line1. It is the item name, then the text " x" with a leading space, then the quantity as text. str(quantity) turns the 3 into "3". The result is Coffee Mug x3. There is a space before the x and none after it.
    3. Build line2. It is "Total: $" followed by str(total). The dollar sign is an ordinary character in the string.
    4. Create result. Join line1, the newline "\n" and line2. The two characters \n in a Python string literal stand for a single newline character, which starts a new row where it sits.

    The solution in Python

    item = "Coffee Mug"
    price = 12
    quantity = 3
    
    total = price * quantity
    line1 = item + " x" + str(quantity)
    line2 = "Total: $" + str(total)
    result = line1 + "\n" + line2
    

    Some details worth knowing:

    • * works on numbers here, but also on strings. price * quantity multiplies two integers. A string times an integer repeats the string, so item * quantity would give "Coffee MugCoffee MugCoffee Mug". Make sure the operands are the numbers.
    • Numbers need str() to be joined. "Total: $" + total raises TypeError: can only concatenate str (not "int") to str.
    • result holds one string. It contains a line break in the middle. print(result) would show two rows, and repr(result) shows 'Coffee Mug x3\nTotal: $36'.
    • An f-string is an alternative. f"{item} x{quantity}" produces the same first row. The lesson uses str() and + so that the conversion is explicit.

    Edge cases and common mistakes

    • Spacing. The first row needs the space before x, and the second row needs the space after the colon. Both live inside the quoted text.
    • Quantity of 1 or 0. The receipt for a quantity of 1 reads Coffee Mug x1, and for 0 it reads Coffee Mug x0 with Total: $0. The code handles both, and does not special-case singular or plural.
    • Hard-coding the numbers. Writing "Total: $36" passes for the starting data but fails once the price or the quantity changes.
    • Adding instead of multiplying. price + quantity gives 15, not 36.
    • Typing /n. A forward slash does not start a new row. The newline is a backslash followed by n.

    Testing

    • item = "Pen", price = 2, quantity = 10 gives Pen x10 then Total: $20.
    • price = 7, quantity = 1 gives Coffee Mug x1 then Total: $7.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free