Overview
Each week, your assignment as an intern is to identify salespeople who exceed the company’s
standard for excellence ($85,000 in sales or more). After spending a few weeks analyzing data and generating reports manually, you’ve decided to try your hand at automating the process. Luckily, your company provides you with access to Python! If successful, you will save both yourself and your fellow interns hours of monotonous work.
Instructions
You must write code that does the following:
Displays an input that requests the file path location of the “Sales_Superstars” Excel sheet.
Reads the “Sales_Superstars” Excel sheet using the file path location that was requested in step 1.
Iterates through every row of the “Employee_Sales” data table that was read in step 2. For every row of data that contains a salesperson who sold over $85,000 worth of goods, add the name of that salesperson to a list.
After iterating through each row of data, display a message box that contains the final list of salespeople who surpassed $85,000 in sales.
I recommend importing both tkinter and tkinter.messagebox to display a message box.
Solution
The following is a list of employees with sales over $85,000:
Clyde Meyer, Anthony Byrd. Melinda Foster, Nina Dolan, Cecil Santos, Omar Thornton, Flora Harris
Solution Code
#import necessary packages
import pandas as pd
import tkinter
import tkinter.messagebox
# create empty list
sales_superstars = []
# request file path location
directory = input('Please input the file path location of the “Sales_Superstars” Excel sheet.\n ')
if directory[-1] != '\\' :
directory = directory + '\\'
# read in “Sales_Superstars” Excel sheet
df = pd.read_excel(directory + 'Challenge27_Sales_Superstars.xlsx')
# iterate through Excel sheet and add salespeople with over $85,000 in sales to list
for index,row in df.iterrows():
name = row['Name']
sales = row['Sales']
if sales > 85000:
sales_superstars.append(name)
# convert list to string for better visual appearance (not necessary)
sales_superstars = ', '.join(sales_superstars)
# display message box with the names of the "Sales Superstars"
tkinter.messagebox.showinfo("Sales Superstars", sales_superstars)
This was a good Python challenge that gave me exposure to tkinter, I hadn’t really used that before! My code had a little less error handling than yours (whoops), so I can obviously improve on that. But overall, I think I did this pretty well. Here’s my code:
import pandas as pd
import tkinter as tk
from tkinter import messagebox
path = input("What is the file path of the Sales_Superstars Excel file? ")
df = pd.read_excel(path)
sales_stars = []
for i, row in df.iterrows():
sales = row['Sales']
if sales >= 85000:
sales_stars.append(row['Name'])
print(sales_stars)
# create a message box
messagebox.showinfo("Sales Superstars", sales_stars)
This is what I got:
import pandas as pd
from tkinter import Tk, filedialog, messagebox
#Step 1: Request the file path location of the Excel sheet
Tk().withdraw() # Hide the main window
file_path = filedialog.askopenfilename(title=“Select the Sales_Superstars Excel file”, filetypes=[(“Excel Files”, “*.xlsx”)])
#Step 4: Display a message box with the final list of sales superstars
if superstars:
message = “Sales Superstars (>$85,000 in sales):\n” + “\n”.join(superstars)
messagebox.showinfo(“Sales Superstars”, message)
try:
# Read the Excel file into a DataFrame
df = pd.read_excel(file_path)
# Create an empty list to store results
high_sales_list = []
# Iterate over the DataFrame rows
for index, row in df.iterrows():
# Assuming the column names are 'Name' and 'Sales'
name = row['Name']
sales = row['Sales']
# Check if the sales number is above 80,000
if sales > 80000:
# Append the person's name and their sales number to the list
high_sales_list.append({'Name': name, 'Sales': sales})
# Print the list of people with sales above 80,000
for item in high_sales_list:
print(f"Name: {item['Name']}, Sales: {item['Sales']}")
except FileNotFoundError:
print(“File not found.”)
except pd.errors.EmptyDataError:
print(“The Excel file is empty or contains no data.”)
except pd.errors.ParserError:
print(“Error occurred while parsing the Excel file.”)
except KeyError:
print(“One or more required columns were not found in the Excel file.”)