Problem overview
Two brands selling the same product, stored in brand1, rating1, brand2 and rating2. The program shows only the one with the higher rating, formatted as a ranked entry, in a string called output. Along the way it keeps the winner's data in best_brand and best_rating.
With the starting data, TechPro is rated 4 and MegaCorp is rated 5, so the winner is MegaCorp:
1. MegaCorp (5)
Approach: decide first, format once
The decision and the formatting are separate jobs. An if/else decides which brand wins and copies its name and rating into best_brand and best_rating. After the branch, one line of string-building produces the ranked entry from those two variables. The formatting code is written once and works for either winner, so it is never duplicated inside the branches.
The condition is rating1 > rating2, a strict comparison. The first brand wins only when it is strictly better. A tie goes to the else branch, which means brand 2 wins ties in this version. With equal ratings the lesson picks brand 2, so the order of the comparison decides ties.
Step by step
- Start the
if. Testrating1 > rating2. Inside it, createbest_brandand set it tobrand1. - Keep the rating with the name. Still inside the
if, setbest_ratingtorating1. A brand and its rating belong together, so each branch sets both. - Write the
else. It runs when brand 1 is not strictly better. Setbest_brandtobrand2. - Finish the
else. Setbest_ratingtorating2. - Build
outputafter the branch, with no indentation: the text"1. ", the best brand," (", the best rating converted withstr(), and")". The winner's variables feed the string, so the recipe is written once.
The solution in Python
brand1 = "TechPro"
rating1 = 4
brand2 = "MegaCorp"
rating2 = 5
if rating1 > rating2:
best_brand = brand1
best_rating = rating1
else:
best_brand = brand2
best_rating = rating2
output = "1. " + best_brand + " (" + str(best_rating) + ")"
Some details worth knowing:
- Indentation marks the branches. The two lines under
ifand the two underelseare indented by the same amount. Theoutputline is not indented, so it runs after whichever branch was taken. - Variables from a branch stay available. Python has no block scope, so
best_brandandbest_ratingcan be used after theif/else. Because both branches assign both names, they always exist. - The rating is an
int. Joining it to a string needsstr(best_rating). Without it, Python raisesTypeError: can only concatenate str (not "int") to str.
Edge cases and common mistakes
- Equal ratings. With ratings of 4 and 4,
rating1 > rating2is false and brand 2 is chosen. Writing>=would hand ties to brand 1 instead. - Setting only one variable per branch. If a branch updates
best_brandbut forgetsbest_rating, the output could show the right name with the wrong number, or fail with aNameError. - Comparing the brand names. The condition has to compare the ratings. Comparing
brand1withbrand2would compare the strings alphabetically. - Formatting inside both branches. It works, but the string recipe then exists twice and any later change must be made in both places.
Testing
rating1 = 5,rating2 = 3gives1. TechPro (5).rating1 = 4,rating2 = 4gives1. MegaCorp (4).
