Start learning free

LESSON 101 OF 673 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V2 in Python: Choosing a Winner with if/else

Practice Read the full explanation

Two employees are up for the Employee of the Year award. Pick the more productive one and compute their 15% bonus.

You start with:

name1 = "John Doe"
productivity1 = 83
salary1 = 135000
name2 = "Mary Jane"
productivity2 = 133
salary2 = 335000

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever employee has the higher productivity.
  • If both have the same productivity, employee 1 (the one listed first) wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Use if/else to pick the winner. Store both their name and their salary (winner_name, winner_salary) in every branch, so the bonus formula stays simple. A tie goes to employee 1, so compare with >=.

Use integer division // so the bonus stays a whole number: bonus = winner_salary * 15 // 100.

Joining text and a number needs str(): print("Bonus: $" + str(bonus))

name1 = "John Doe"
productivity1 = 83
salary1 = 135000
name2 = "Mary Jane"
productivity2 = 133
salary2 = 335000

if productivity1 >= productivity2:
    winner_name = name1
    winner_salary = salary1
else:
    winner_name = name2
    winner_salary = salary2
bonus = winner_salary * 15 // 100
print(winner_name)
print("Bonus: $" + str(bonus))

STEP 1 OF 7
    Ctrl + Enter to check

    PYTHON · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in Python, explained

    Problem overview

    Version 2 of the Employee of the Year program has two employees, each with a name, a productivity score and a salary, stored in the variables name1, productivity1, salary1, name2, productivity2 and salary2. The more productive employee wins. If the two scores are equal, employee 1, the one listed first, wins. The winner gets a bonus of 15% of their salary, rounded down, and the program prints two lines: the winner's name and Bonus: $ followed by the amount.

    With the starting data, John Doe has productivity 83 and Mary Jane has 133, so Mary Jane wins. Her salary is 335000, and 15% of that is 50250:

    Mary Jane
    Bonus: $50250

    The bonus formula is the one from the previous version. The new part is deciding whose data to use.

    Approach: store the winner, then do the rest once

    There are two possible winners, and the bonus and printing steps are the same for both. So the program should not repeat them in each branch. Instead, an if/else copies the winner's name and salary into two new variables, winner_name and winner_salary. After the branch, the rest of the code works only with those two variables.

    The condition is productivity1 >= productivity2. It uses >= rather than > so that a tie falls into the if branch, which belongs to employee 1. The else branch then runs only when employee 2 is strictly more productive.

    Step by step

    1. Open the if. Test productivity1 >= productivity2 and, indented inside it, set winner_name to name1.
    2. Add the salary to the same branch. Set winner_salary to salary1. The name and the salary always travel together, so a branch sets both before it ends.
    3. Write the else. It lines up with the if and has no condition of its own. Inside it, set winner_name to name2.
    4. Finish the else. Set winner_salary to salary2.
    5. Compute the bonus without indentation, so it runs after the branch whichever one was taken: winner_salary * 15 // 100. As in the previous version, // keeps it a whole number.
    6. Print the name with print(winner_name).
    7. Print the bonus line by joining "Bonus: $" and str(bonus). A number has to be converted to text before + can join it to a string.

    The solution in Python

    name1 = "John Doe"
    productivity1 = 83
    salary1 = 135000
    name2 = "Mary Jane"
    productivity2 = 133
    salary2 = 335000
    
    if productivity1 >= productivity2:
        winner_name = name1
        winner_salary = salary1
    else:
        winner_name = name2
        winner_salary = salary2
    bonus = winner_salary * 15 // 100
    print(winner_name)
    print("Bonus: $" + str(bonus))
    

    Some details worth knowing:

    • Indentation is the block. Python has no braces. The lines indented under if and else belong to them, and the first unindented line, bonus = ..., ends the branch. Indenting that line by mistake would put it inside the else, and a win for employee 1 would then crash with a NameError when the program reaches the last print.
    • No block scope. A variable assigned inside an if or else is an ordinary variable of the surrounding code. After the branch, winner_name exists, whichever branch created it. This only works because both branches assign both variables. If one branch skipped an assignment, using the variable afterwards could fail.
    • The else needs no test. If productivity1 >= productivity2 is false, then productivity2 is larger, so a second comparison would be redundant.
    • Comparison operators. >= means greater than or equal, and a single = is assignment. Writing if productivity1 = productivity2: is a syntax error.

    Tracing the example

    Python first runs the six assignments, so all six variables exist. Then it evaluates the condition: 83 >= 133 is false, so the if branch is skipped and the else branch runs. That sets winner_name to "Mary Jane" and winner_salary to 335000. The bonus line computes 335000 * 15, which is 5025000, and floor-divides it by 100 to get 50250. The two print calls then write the name and the text Bonus: $50250. Employee 1's data was never copied, which is exactly what the condition decided. Only the values of winner_name and winner_salary reach the last three lines, so those lines never need to know which branch ran.

    Edge cases and common mistakes

    • Ties. With productivity1 = 100 and productivity2 = 100, employee 1 wins. Using > would hand the win to employee 2.
    • Setting only the name. If a branch forgets winner_salary, the bonus is computed from a missing or stale value. Keep the pair together.
    • Taking the bonus from the productivity. The bonus depends on the salary of the winner, not on the score and not on the loser's salary.
    • Repeating the printing in each branch. It works, but the same three lines then exist twice. Storing the winner first keeps the code shorter and easier to extend.

    Testing

    • Swap the scores (productivity1 = 133, productivity2 = 83): John Doe wins, and the bonus is 135000 * 15 // 100, so the output is John Doe and Bonus: $20250.
    • Equal scores of 90: employee 1 wins, so the first name is printed.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free