Start learning free

LESSON 103 OF 673 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 in Python: Picking the Better Brand with if/else

Practice Read the full explanation

Two brands sell the same product. Show the one with the higher rating.

You start with:

brand1 = "TechPro"
rating1 = 4
brand2 = "MegaCorp"
rating2 = 5

Build:

1. MegaCorp (5)

Rules:

  • Use if/else to pick the winner: brand1 when rating1 > rating2, otherwise brand2.
  • Store the winner's name and rating in best_brand and best_rating.
  • Format the result as "1. Brand (rating)" and store it in output.

Pick the winner into best_brand and best_rating first, then build the output text from them.

In Python a number must go through str() before it can be joined to text.

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) + ")"

STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · CONDITIONAL STATEMENTS - CHALLENGES

    Top K Brands V2 in Python, explained

    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

    1. Start the if. Test rating1 > rating2. Inside it, create best_brand and set it to brand1.
    2. Keep the rating with the name. Still inside the if, set best_rating to rating1. A brand and its rating belong together, so each branch sets both.
    3. Write the else. It runs when brand 1 is not strictly better. Set best_brand to brand2.
    4. Finish the else. Set best_rating to rating2.
    5. Build output after the branch, with no indentation: the text "1. ", the best brand, " (", the best rating converted with str(), 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 if and the two under else are indented by the same amount. The output line is not indented, so it runs after whichever branch was taken.
    • Variables from a branch stay available. Python has no block scope, so best_brand and best_rating can be used after the if/else. Because both branches assign both names, they always exist.
    • The rating is an int. Joining it to a string needs str(best_rating). Without it, Python raises TypeError: can only concatenate str (not "int") to str.

    Edge cases and common mistakes

    • Equal ratings. With ratings of 4 and 4, rating1 > rating2 is 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_brand but forgets best_rating, the output could show the right name with the wrong number, or fail with a NameError.
    • Comparing the brand names. The condition has to compare the ratings. Comparing brand1 with brand2 would 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 = 3 gives 1. TechPro (5).
    • rating1 = 4, rating2 = 4 gives 1. MegaCorp (4).
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free