Start learning free

LESSON 95 OF 410 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 - Better of Two in Python

Practice

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

    Top K Brands V2 in Python

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free