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
- Compute
total. Writeprice * quantity. Both are integers, so the result is an integer: 36. - 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 isCoffee Mug x3. There is a space before thexand none after it. - Build
line2. It is"Total: $"followed bystr(total). The dollar sign is an ordinary character in the string. - Create
result. Joinline1, the newline"\n"andline2. The two characters\nin 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 * quantitymultiplies two integers. A string times an integer repeats the string, soitem * quantitywould give"Coffee MugCoffee MugCoffee Mug". Make sure the operands are the numbers.- Numbers need
str()to be joined."Total: $" + totalraisesTypeError: can only concatenate str (not "int") to str. resultholds one string. It contains a line break in the middle.print(result)would show two rows, andrepr(result)shows'Coffee Mug x3\nTotal: $36'.- An f-string is an alternative.
f"{item} x{quantity}"produces the same first row. The lesson usesstr()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 readsCoffee Mug x0withTotal: $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 + quantitygives 15, not 36. - Typing
/n. A forward slash does not start a new row. The newline is a backslash followed byn.
Testing
item = "Pen",price = 2,quantity = 10givesPen x10thenTotal: $20.price = 7,quantity = 1givesCoffee Mug x1thenTotal: $7.
