Start learning free

LESSON 55 OF 287 · CALCULATIONS - CHALLENGES

Employee of the Year V1 in Python: Percentages and Printing

Practice Read the full explanation

Compute an end-of-year performance bonus for one employee, then print the employee's name and the bonus.

You start with:

name = "John Doe"
productivity = 83
salary = 135000

Build:

John Doe
Bonus: $20250

Rules:

  • The bonus is 15% of the salary, rounded down to a whole number.
  • Print the employee's name on the first line.
  • Print the bonus on the second line as "Bonus: $<amount>".
  • productivity is not used in this version. It is there for the next versions, where you will compare employees by productivity.

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

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

name = "John Doe"
productivity = 83
salary = 135000

bonus = salary * 15 // 100
print(name)
print("Bonus: $" + str(bonus))

STEP 1 OF 3
    Ctrl + Enter to check

    PYTHON · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in Python, explained

    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

    1. Create bonus. Write salary * 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.
    2. Print the name. print(name) writes the string followed by a line break, so it becomes the first line.
    3. Print the bonus line. The text "Bonus: $" and the number bonus have to become one string. The + operator joins strings only, so the number goes through str() 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 * 15 divides first and loses the remainder too early. For a salary of 150 it gives 15, while 150 * 15 // 100 gives 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: $" + bonus raises TypeError: 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 uses str() 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 reads Bonus: $20250.0.
    • Forgetting the conversion. Joining the number directly raises a TypeError and the program stops before printing the second line.
    • A missing space. The output is Bonus: $20250 with 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 print calls.
    • 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 = 100000 prints Bonus: $15000.
    • salary = 12345 prints Bonus: $1851.
    • salary = 0 prints Bonus: $0.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free