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
- Open the
if. Testproductivity1 >= productivity2and, indented inside it, setwinner_nametoname1. - Add the salary to the same branch. Set
winner_salarytosalary1. The name and the salary always travel together, so a branch sets both before it ends. - Write the
else. It lines up with theifand has no condition of its own. Inside it, setwinner_nametoname2. - Finish the
else. Setwinner_salarytosalary2. - 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. - Print the name with
print(winner_name). - Print the bonus line by joining
"Bonus: $"andstr(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
ifandelsebelong to them, and the first unindented line,bonus = ..., ends the branch. Indenting that line by mistake would put it inside theelse, and a win for employee 1 would then crash with aNameErrorwhen the program reaches the lastprint. - No block scope. A variable assigned inside an
iforelseis an ordinary variable of the surrounding code. After the branch,winner_nameexists, 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
elseneeds no test. Ifproductivity1 >= productivity2is false, thenproductivity2is larger, so a second comparison would be redundant. - Comparison operators.
>=means greater than or equal, and a single=is assignment. Writingif 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 = 100andproductivity2 = 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 isJohn DoeandBonus: $20250. - Equal scores of 90: employee 1 wins, so the first name is printed.
