Problem overview
This is the first version of a small program that is built over several versions. Here it handles a single employee. The data is already in three variables: name, productivity and salary. The program computes a year-end bonus of 15% of the salary, rounded down to a whole number, and prints two lines: the name, then the bonus in the form Bonus: $20250.
With the starting data, the salary is 135000. Fifteen percent of that is 20250, so the output is:
John Doe
Bonus: $20250
The variable productivity is not used yet. The statement says it is there for later versions, where employees are compared by productivity.
Approach: integer math, then text
The job has two parts. First calculate the bonus as a whole number. Then build the text for the second line, because Python will not join a string and a number on its own.
For the calculation, multiply the salary by 15 and divide by 100. Use the floor-division operator //, which divides and drops everything after the decimal point. Ordinary division with / always returns a float in Python, so 135000 * 15 / 100 gives 20250.0, and printing that would show Bonus: $20250.0, which does not match the required output.
Step by step
- Create
bonus. Writesalary * 15 // 100. Multiplication and//have the same precedence and are evaluated left to right, so the salary is multiplied by 15 first and the result is divided by 100. - Print the name.
print(name)writes the string followed by a line break, so it becomes the first line. - Print the bonus line. The text
"Bonus: $"and the numberbonushave to become one string. The+operator joins strings only, so the number goes throughstr()first. The dollar sign is an ordinary character inside the quotes and needs no special treatment.
The solution in Python
name = "John Doe"
productivity = 83
salary = 135000
bonus = salary * 15 // 100
print(name)
print("Bonus: $" + str(bonus))
Some details worth knowing:
- Multiply before dividing. Doing
salary // 100 * 15divides first and loses the remainder too early. For a salary of 150 it gives 15, while150 * 15 // 100gives 22, which is 22.5 rounded down. The salary in this lesson is a multiple of 100, so both orders match here, but only the multiply-first order is right in general. str()is required."Bonus: $" + bonusraisesTypeError: can only concatenate str (not "int") to str. Python does not guess whether you meant addition or joining text.- An f-string is an alternative.
print(f"Bonus: ${bonus}")prints the same text and converts the number for you. The lesson usesstr()and+to make the conversion visible. - Rounding down.
//floors the result. For positive salaries that is the same as cutting off the decimals. For example, a salary of 12345 gives a bonus of 1851, since 15% of 12345 is 1851.75.
Edge cases and common mistakes
- Using
/instead of//. The bonus becomes a float, and the output readsBonus: $20250.0. - Forgetting the conversion. Joining the number directly raises a
TypeErrorand the program stops before printing the second line. - A missing space. The output is
Bonus: $20250with one space after the colon. That space belongs inside the string, in"Bonus: $". - Printing the name inside the same call. The name and the bonus go on two separate lines, so there are two
printcalls. - Using
productivity. It plays no part in this version. The bonus depends only on the salary.
Testing
Change the starting data and compare against the arithmetic:
salary = 100000printsBonus: $15000.salary = 12345printsBonus: $1851.salary = 0printsBonus: $0.
