155|PYTHON – Retail Analysis

BYU Student Author: @Kyle_Nilsen
Reviewers: @Millie_K_B, @Trent_Barlow
Estimated Time to Solve: 15 Minutes

This is an intro challenge that is part of the Python Learning Path.

We provide the solution to this challenge using:

  • Python

Need a program? Click here.

Introduction
As part of our Python Learning Path, we have created both a learning document and a final challenge to be completed for this subtopic. While you do not need to complete the learning document to try the challenge or post a solution, we highly recommend doing so. The learning document and the solution to its practice problems can be found here:

Overview
You are working for a retail company and have been tasked with analyzing the sales data from different stores for the 2023 fiscal year. Specifically, your manager is interested in seeing which days report the best and worst sales for the entire year and what those amounts are. Beyond this, she wants to know the average amount of revenue generated from the company’s activities.

Instructions
The sales data is stored in a list of dictionaries, where each dictionary represents sales for a specific day. Each dictionary contains information about the day (date) and the sales amount for that day. Using this data, create different variables and loops to arrive at these important results. Some extra instructions are included in the jupyter notebook.

  1. Use a for loop to calculate the total sales for the year
  2. Calculate the average daily sales using the total sales found in the previous step divided by the number of days in a year (This can be found by calculating the length of the sales_data list)
  3. Create your best_day, best_day_sales, worst_day, and worst_day sales variables. As a hint, worst_day sales cannot be initialized as 0, as there will never be a day that makes 0 sales. You can create this either by initializing it as the upper bound of sales (2000) or with float(‘inf’), which is positive infinity. The first sale iteration should then immediately become the worst day.
  4. Using a while loop, discover and assign to your previously created variables what the best and worst days were for the company.

Data Files

Suggestions and Hints
  1. The total_sales can be found by iterating through the sales_data list, looking specifically at the “sales” key in the dictionary.
    total_sales = 0
    for day in sales_data:
    total_sales += day[“sales”]
  2. To find the best and worst days, you need to compare what days have already been through the loop to the present iteration. You also need to make sure that every day of the year is analyzed. I called my iteration item “index,” as it makes it clear that the current index iteration will be used to capture the correct values.
  3. Because sales_data is a list of dictionaries with two keys per dictionary, you will first need to call the index variable previously set (the day we are on with its corresponding sales), and then next by the relevant key. For example, to retrieve the sales from that day, the code would be sales_data[index][“sales”]. After this data is retrieved, you can compare it to the value previously stored in best and worst day variables. Make sure to update the best-selling and worst-selling days as well, accessed with [“date”]. If you’re still stuck, below is my code for the while loop to find the best and worst days.
    index = 0
    while index < len(sales_data):
    if sales_data[index][“sales”] > best_day_sales:
    best_day_sales = sales_data[index][“sales”]
    best_day = sales_data[index][“date”]
    if sales_data[index][“sales”] < worst_day_sales:
    worst_day_sales = sales_data[index][“sales”]
    worst_day = sales_data[index][“date”]
    index += 1

Solution

As the sales_data list is comprised of randomly generated sales numbers, there is no consistent number for output against which you can check your answer. That said, the best_day_sales will likely be in the high 1900’s, worst_day_sales will likely be in the low 500’s, and the average_sales will likely be in the mid 1200’s.
Challenge155_Solution.ipynb
Solution Video: Challenge 155|PYTHON – Retail Analysis

I really enjoyed this challenge! I’m not very comfortable with while loops, so I used a for loop and an iterator based off of the range between 0 and the length of the sales data list instead, but I want to try Kyle’s while loop next time.

Time to complete: 7 minutes
Rating: Beginner

I really enjoyed this challenge. It took me a minute to remember how to do min/max of dictionaries like this.

Time: 15 Minutes