Start learning free

LESSON 102 OF 673 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V3 in Python: if, elif and else for Three Employees

Practice Read the full explanation

Three employees compete for the award. Use a chained if / elif / else to pick the most productive one and print their 15% bonus.

You start with:

name1 = "John Doe"
productivity1 = 83
salary1 = 135000
name2 = "Mary Jane"
productivity2 = 133
salary2 = 335000
name3 = "Bob Smith"
productivity3 = 97
salary3 = 210000

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever of the three employees has the highest productivity.
  • If two or more employees share the highest productivity, 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 an if/elif/else chain to pick the winner. Store both their name and their salary (winner_name, winner_salary) in every branch, so the bonus formula stays simple. Employee 1 wins when its productivity is at least that of both others; if not, employee 2 wins when it is at least employee 3's; otherwise employee 3 wins.

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
name3 = "Bob Smith"
productivity3 = 97
salary3 = 210000

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

STEP 1 OF 9
    Ctrl + Enter to check

    PYTHON · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V3 in Python, explained

    Problem overview

    This version has three employees. The variables are name1, productivity1, salary1, and the same three for employees 2 and 3. The winner is the employee with the highest productivity. When several employees share the highest score, the one listed first wins. The winner's bonus is 15% of their salary, rounded down, and the program prints the winner's name followed by Bonus: $ and the amount.

    In the starting data the scores are 83, 133 and 97, so Mary Jane wins with a salary of 335000, and the output is:

    Mary Jane
    Bonus: $50250

    With three employees, a single if/else is not enough, and this lesson uses elif to chain the tests.

    Approach: ask "is it employee 1?", then "is it employee 2?"

    A chain of conditions works from the top down, and Python runs only the first branch whose condition is true. That order does the logical work for you.

    The first test asks whether employee 1 is at least as productive as both of the others. If so, employee 1 wins, and ties go to employee 1 as required. If not, somebody else beats employee 1, so employee 1 is out. The second test therefore only has to compare employee 2 with employee 3: if employee 2 is at least as productive as employee 3, employee 2 wins. Otherwise employee 3 is strictly better than employee 2 and also beat employee 1, so the final else declares employee 3 the winner without any further test.

    Every comparison uses >=, so the earlier employee wins any tie. Employee 2 against employee 3 is the only tie that the second test sees, and it favours employee 2, who is listed first of the two.

    Step by step

    1. Start the if for employee 1. The condition combines two comparisons with and: productivity1 must be at least productivity2 and at least productivity3. Inside, set winner_name to name1.
    2. Store the salary in the same branch: winner_salary = salary1.
    3. Add the elif. It checks only productivity2 >= productivity3. Reaching this line already proves employee 1 lost. Inside, set winner_name to name2.
    4. Store employee 2's salary in winner_salary.
    5. Add the else. It handles the remaining case and has no condition. Set winner_name to name3.
    6. Store employee 3's salary.
    7. Compute the bonus after the whole chain, with no indentation: winner_salary * 15 // 100.
    8. Print the name, then print "Bonus: $" joined with str(bonus).

    The solution in Python

    name1 = "John Doe"
    productivity1 = 83
    salary1 = 135000
    name2 = "Mary Jane"
    productivity2 = 133
    salary2 = 335000
    name3 = "Bob Smith"
    productivity3 = 97
    salary3 = 210000
    
    if productivity1 >= productivity2 and productivity1 >= productivity3:
        winner_name = name1
        winner_salary = salary1
    elif productivity2 >= productivity3:
        winner_name = name2
        winner_salary = salary2
    else:
        winner_name = name3
        winner_salary = salary3
    bonus = winner_salary * 15 // 100
    print(winner_name)
    print("Bonus: $" + str(bonus))
    

    Some details worth knowing:

    • elif is "else, but only if". It is short for an else followed by a nested if. The branches of a chain are mutually exclusive: once one runs, Python skips the remaining tests. That is why the second condition can ignore employee 1.
    • and needs both sides true. The first condition uses productivity1 >= productivity2 and productivity1 >= productivity3. Chained comparison would also work in Python, but two explicit comparisons joined by and are easier to read for a beginner.
    • The final else has no test on purpose. By the time Python reaches it, employee 1 lost and employee 2 lost to employee 3 strictly, so employee 3 must be the winner.
    • Every branch assigns both variables. That guarantees winner_name and winner_salary exist after the chain, whichever branch ran.

    Tracing the example

    The scores are 83, 133 and 97. The first condition tests 83 >= 133, which is false, so Python does not even evaluate the second half of the and. It moves to the elif: 133 >= 97 is true, so employee 2 wins. The name is "Mary Jane" and the salary is 335000. The bonus is 335000 * 15 // 100, which is 50250.

    Edge cases and common mistakes

    • Three-way ties. With scores of 90, 90 and 90, the first condition is true, so employee 1 wins.
    • A tie between employees 2 and 3 only. With scores 50, 120 and 120, employee 1 fails the first test, and the elif sees 120 >= 120, so employee 2 wins.
    • Employee 3 winning. With scores 50, 60 and 70, both earlier tests fail, and the else picks employee 3.
    • Using > instead of >=. A tie at the top would then slip past the earlier employees. For scores 100, 100 and 50, a strict first test would fail, and the elif would name employee 2.
    • Comparing only neighbours. Testing employee 1 against employee 2 alone would let employee 1 win even when employee 3 is better.
    • Putting the bonus inside a branch. Then two of the three outcomes would never compute it.

    Testing

    • Scores 150, 120, 100 give employee 1: John Doe and Bonus: $20250.
    • Scores 50, 60, 70 give employee 3: Bob Smith and Bonus: $31500.
    • Scores 100, 100, 50 give John Doe.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free