Problem overview
This project is an e-commerce search page: when a shopper searches a product, it shows the top-rated brand selling it. Version 1 is the smallest case, one product and one brand, and it is all about building text. The starting variables are product, brand and rating.
The program must produce a string called output that has two lines: the product name, then a ranked entry written as 1. Brand (rating). With the starting data the string reads:
Laptop
1. TechPro (4)
Nothing is printed in this version. The result lives in output.
Approach: one variable per line
Instead of building the whole string in one long expression, the lesson makes each line its own variable, product_line and brand_line, and joins them at the end. Small pieces are easier to read and easier to fix when one of them is wrong.
The only language feature that matters here is how Python builds strings. The + operator joins two strings. It does not join a string and a number, so rating, which is an int, has to be converted with str() first.
Step by step
- Create
product_line. The first line is just the product name, so it is set toproduct. No other text is added. - Create
brand_line. It is assembled from five pieces in order: the rank and its dot with a trailing space, the brand, a space and an opening parenthesis, the rating converted to text, and a closing parenthesis. Pay attention to the spaces: one after1.and one before(. - Create
output. Joinproduct_line, the newline character"\n"andbrand_line. The escape\nis a single character that starts a new line wherever it sits in the string.
The solution in Python
product = "Laptop"
brand = "TechPro"
rating = 4
product_line = product
brand_line = "1. " + brand + " (" + str(rating) + ")"
output = product_line + "\n" + brand_line
Some details worth knowing:
- The literal parts keep their spaces. The pieces are
"1. ",brand," (",str(rating)and")". Joined, they give1. TechPro (4). Dropping the space inside" ("would give1. TechPro(4). - Strings are immutable. Each
+creates a new string and leaves the old ones unchanged. That is whyproduct_linecan be reused later without surprises. - Newline inside the string.
outputholds one string with a line break in the middle. If you ranprint(output)it would show two lines, whilerepr(output)shows'Laptop\n1. TechPro (4)'. - An f-string gives the same line.
f"1. {brand} ({rating})"converts the number for you. The lesson uses+andstr()so that each piece is visible.
Edge cases and common mistakes
- Joining a number without
str()." (" + ratingraisesTypeError: can only concatenate str (not "int") to str. - Typing
/ninstead of\n. A forward slash is just a character, so the output would contain a literal/nand stay on one line. - Hard-coding the answer. Writing
"1. TechPro (4)"passes for the starting data but fails as soon asbrandorratingchanges. Build the line from the variables. - Extra spaces or a trailing newline. The expected text has no space at the end of a line and no newline after the last line.
Testing
product = "Phone",brand = "NeoByte",rating = 5givesPhonethen1. NeoByte (5).rating = 10works the same way:str(10)is"10", so the entry ends in(10).
