A challenge: FizzBuzz

Day 6: Exercise Solutions

Python Guru with a screen instead of a face, typing on a computer keyboard with a light orange background to match the day 6 image.

Here are our solutions for the day 6 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!

1) Below we've provided a list of tuples, where each tuple contains details about an employee of a shop. Print how much each employee is due to be paid at the end of the week in a nice, readable format.

employees = [
    ("Rolf Smith", 35, 8.75),
    ("Anne Pun", 30, 12.50),
    ("Charlie Lee", 50, 15.50),
    ("Bob Smith", 20, 7.00)
]

First things first, we're trying to perform some action for each item in a collection, so we know we need a for loop. In this case we're trying to print something for every item in employees.

In this case my for loop is going to look like this:

for employee in employees:

One important thing to keep in mind here is that Python doesn't magically know the meaning of the terms we've used. The name employee is really just a regular variable name that we're going to use for each item that we get given from the employees list.

If we wanted to, we could call it e, or worker: it's just a name. What's important is that we pick something descriptive to make our code easy to understand.

Now that we have the skeleton of our for loop set up, we need to put something in the loop body. This is where we put the actions we want to perform.

The first thing I want to do for each employee is find out their total pay. This is going to be the number of hours they worked, multiplied by their hourly wage. These are the second and third items in each tuple, and we're going to access those values by index.

for employee in employees:
    total_pay = employee[1] * employee[2]

Finally, we need to print the information. I'm going to use an f-string, but you can use a different method if you prefer.

for employee in employees:
    total_pay = employee[1] * employee[2]
    print(f"{employee[0]} has to be paid £{total_pay}")

If we want to make this look a little bit nicer, we can make use of some additional formatting options available to us in Python. To print our total_pay value to two decimal places, we can do this:

for employee in employees:
    total_pay = employee[1] * employee[2]
    print(f"{employee[0]} has to be paid £{total_pay:.2f}")

We talk about this in more detail on our blog.

2) For the employees above, print out those who are earning an hourly wage above average.

This is an exercise we're going to have to tackle in two steps. First, we need to actually calculate the average wage, and then we're going to have to compare each employee's wage against this average we've calculated.

Let's start with getting the average wage. To calculate the average, we need two things. We need the sum of all the wages, and we need to know how many employees we have.

The wages are the third item in each tuple, so we need to loop over the employees list, and somehow add together the items at index 2 for each tuple.

This is how I'm going to do it:

employees = [
    ("Rolf Smith", 35, 8.75),
    ("Anne Pun", 30, 12.50),
    ("Charlie Lee", 50, 15.50),
    ("Bob Smith", 20, 7.00)
]

total = 0

for employee in employees:
    total = total + employee[2]

Here I've created a variable called total, which starts at 0, and then we add the employee's wages to this total one at a time. By time we're done, total contains the sum of all the wages.

Next we need to know how many employees we have. We can use another counter for this.

employees = [
    ("Rolf Smith", 35, 8.75),
    ("Anne Pun", 30, 12.50),
    ("Charlie Lee", 50, 15.50),
    ("Bob Smith", 20, 7.00)
]

total = 0
count = 0

for employee in employees:
    total = total + employee[2]
    count = count + 1

This time the counter starts at 0, and we add 1 every time we encounter a new employee in the loop. This means that by time we're done, counter contains the number of employees in our list.

We can now calculate the average wage like so:

employees = [
    ("Rolf Smith", 35, 8.75),
    ("Anne Pun", 30, 12.50),
    ("Charlie Lee", 50, 15.50),
    ("Bob Smith", 20, 7.00)
]

total = 0
count = 0

for employee in employees:
    total = total + employee[2]
    count = count + 1

average_wage = total / count

Now we need to compare the wage of each employee against the average wage that we calculated. We can use another loop for this, and inside we can put a conditional statement to compare the employee's wage against the average.

If the employee's wage exceeds the average, we can print out a message to indicate this:

employees = [
    ("Rolf Smith", 35, 8.75),
    ("Anne Pun", 30, 12.50),
    ("Charlie Lee", 50, 15.50),
    ("Bob Smith", 20, 7.00)
]

total = 0
count = 0

for employee in employees:
    total = total + employee[2]
    count = count + 1

average_wage = total / count

for employee in employees:
    if employee[2] > average_wage:
        print(f"{employee[0]} earns more than average.")

There are a couple of things we can do to make this a little neater. First, we can use the += operator, like so:

total += employee[2]

This is a shorthand for writing this:

total = total + employee[2]

We can use this in a couple of places in our solution.

employees = [
    ("Rolf Smith", 35, 8.75),
    ("Anne Pun", 30, 12.50),
    ("Charlie Lee", 50, 15.50),
    ("Bob Smith", 20, 7.00)
]

total = 0
count = 0

for employee in employees:
    total += employee[2]
    count += 1

average_wage = total / count

for employee in employees:
    if employee[2] > average_wage:
        print(f"{employee[0]} earns more than average.")

Secondly, there's another way we can find the number of items in a collection. We can use the len function. When we pass a collection to len, it will tell us how many elements are inside.

This means we don't need our count variable anymore.

employees = [
    ("Rolf Smith", 35, 8.75),
    ("Anne Pun", 30, 12.50),
    ("Charlie Lee", 50, 15.50),
    ("Bob Smith", 20, 7.00)
]

total = 0

for employee in employees:
    total += employee[2]

average_wage = total / len(employees)

for employee in employees:
    if employee[2] > average_wage:
        print(f"{employee[0]} earns more than average.")

We'll be talking about len in more detail tomorrow.