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
- Start the
iffor employee 1. The condition combines two comparisons withand:productivity1must be at leastproductivity2and at leastproductivity3. Inside, setwinner_nametoname1. - Store the salary in the same branch:
winner_salary = salary1. - Add the
elif. It checks onlyproductivity2 >= productivity3. Reaching this line already proves employee 1 lost. Inside, setwinner_nametoname2. - Store employee 2's salary in
winner_salary. - Add the
else. It handles the remaining case and has no condition. Setwinner_nametoname3. - Store employee 3's salary.
- Compute the bonus after the whole chain, with no indentation:
winner_salary * 15 // 100. - Print the name, then print
"Bonus: $"joined withstr(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:
elifis "else, but only if". It is short for anelsefollowed by a nestedif. 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.andneeds both sides true. The first condition usesproductivity1 >= productivity2 and productivity1 >= productivity3. Chained comparison would also work in Python, but two explicit comparisons joined byandare easier to read for a beginner.- The final
elsehas 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_nameandwinner_salaryexist 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
elifsees 120 >= 120, so employee 2 wins. - Employee 3 winning. With scores 50, 60 and 70, both earlier tests fail, and the
elsepicks 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 theelifwould 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 DoeandBonus: $20250. - Scores 50, 60, 70 give employee 3:
Bob SmithandBonus: $31500. - Scores 100, 100, 50 give
John Doe.
