python

Make your own Python business sales program with just the HyperionDev Free Trial basics

Posted by

In this series, we learn how to make simple Python programs for real-world problems using just the material taught in HyperionDev’s free trial. Today, we break down how to code a basic program that goes through a confusing, pages-long text document of sales information to give a business owner his total sales, total number of items sold, and tips payable to his workers. 

The real-world problem: a broken sales machine

Let’s imagine the scene: your friend John runs a hot food stall at club rugby games on the weekend. His stand is doing very well, and food is flying off the shelf… when his point of sales machine suddenly breaks. Not wanting to stop trade to wait to fix the machine, he tells his staff to just write down their names, the items they sold, and whatever tips they made on a notepad on his tablet.

Of course, after a day of hectic sales, he checks the list and sees that it’s a dense mess, with 500+ sales made, and thousands of items in the note. He doesn’t have the time to sit there and do it all by hand, so he calls you, asking if you can somehow pull useful information from the confusing text mess. John wants to know the key info about how much his stall sold of each item, how much money he made, and how much he has to pay out to each person who manned the tills that day.

John is in luck, because this is exactly the kind of work that Python makes quick and painless. 

 

Setting up what you need for the program

To start, let’s give John’s sales information to the program . We can do this by creating a string that is equal to the text of John’s iPad sales note (for brevity, the example we’re using is only 50 sales items long, but this code will work on a similar file that’s thousands of items long):

daily_sales = “””Khatija, boerie, boerie, B&E, B&E,  chips,  chips, water, coke, coke, water, 20 ; Khatija,  chips, B&E, boerie,  chips, boerie,  chips, water, water, coffee, coke, coffee, water, 24 ; Khatija, burger, B&E,  chips, water, water, water, coke, coffee, coffee, coffee, 20 ; Noxi, burger, burger, boerie, B&E, B&E,  chips, burger, water, 16 ; Noxi,  chips, boerie,  chips, B&E, burger, water, water, coffee, 16 ; Khatija, boerie, boerie,  chips, burger,  chips,  chips, B&E, water, coke, 18 ; Fred, B&E,  chips, B&E, coffee, water, 10 ; Fred, B&E, burger, boerie,  chips, boerie, boerie, water, coffee, coffee, water, coffee, coffee, 24 ; Khatija, boerie, burger, B&E, burger, B&E, burger, water, coffee, coffee, 18 ; Khatija, burger, boerie, boerie, burger, burger,  chips, coke, coffee, coke, water, coke, coffee, 24 ; Khatija,  chips, B&E, B&E, B&E, boerie, B&E, water, coffee, coke, coffee, 20 ; Noxi, burger,  chips, B&E, boerie, burger, water, 12 ; Fred, boerie, boerie, boerie,  chips, boerie, boerie, B&E, coke, water, coke, coffee, water, coke, 26 ; Noxi, B&E, burger, B&E, boerie, burger, burger, burger, coke, water, coffee, 20 ; Noxi, burger, boerie, burger, burger, boerie,  chips,  chips, water, water, coke, coke, coffee, 24 ; Khatija,  chips, water, water, coke, coffee, coke, coke, water, 16 ; Noxi, B&E, boerie,  chips, boerie, burger, coffee, water, coffee, coke, coke, coffee, 22 ; Fred, boerie, boerie,  chips, boerie, B&E, B&E, coffee, water, coffee, 18 ; Fred, burger,  chips, burger, burger, coke, coke, coffee, water, 16 ; Noxi, boerie,  chips, burger, burger, burger, coffee, 12 ; Fred, burger,  chips,  chips, water, 8 ; Noxi, B&E,  chips, boerie, burger,  chips, boerie, water, coke, coke, coke, 20 ; Khatija, B&E,  chips, burger,  chips, B&E, boerie, coffee, coffee, 16 ; Fred, boerie, boerie, burger, boerie,  chips, water, coffee, coke, coke, water, coffee, water, 24 ; Khatija,  chips, B&E, B&E, boerie,  chips, boerie, boerie, water, water, coffee, water, coffee, 24 ; Noxi, B&E, B&E, coffee, coke, coke, water, coke, 14 ; Khatija, boerie, B&E,  chips,  chips,  chips, boerie, boerie, coke, water, 18 ; Fred, burger,  chips,  chips,  chips, burger, B&E, coffee, coffee, coke, 18 ; Khatija, burger, burger, coffee, coffee, coke, water, coffee, 14 ; Fred, burger, boerie, water, coke, coffee, coke, 12 ; Khatija, boerie, B&E,  chips, burger, B&E,  chips, B&E, coffee, coffee, coke, coke, water, coke, 26 ; Fred, boerie, boerie, B&E, water, water, 10 ; Fred, boerie, burger,  chips,  chips,  chips, water, water, coffee, water, coffee, 20 ; Noxi, burger, B&E, boerie,  chips, burger, coke, coffee, coffee, coffee, coffee, coffee, coke, 24 ; Khatija, B&E, boerie,  chips, water, coffee, water, water, coke, coke, 18 ; Noxi, boerie, B&E, coffee, coke, coke, coke, coke, 14 ; Fred, boerie, water, coffee, coffee, coke, 10 ; Noxi, B&E, water, coke, 6 ; Fred,  chips,  chips, boerie,  chips, coke, coffee, water, coffee, 16 ; Khatija,  chips, boerie, burger,  chips, coke, 10 ; Fred, burger, boerie, boerie, burger, burger, burger, B&E, water, coke, 18 ; Noxi,  chips, burger, burger,  chips, B&E, water, 12 ; Fred,  chips, B&E, coffee, coke, 8 ; Noxi, boerie, boerie, boerie, burger, coke, 10 ; Khatija, B&E,  chips, B&E, coffee, water, coke, 12 ; Fred, boerie, boerie, B&E, boerie, B&E,  chips, burger, coke, water, water, coke, coke, water, coke, 28 ; Khatija, B&E, coffee, coffee, coffee, 8 ; Noxi, B&E, boerie, burger, B&E, B&E, B&E, water, water, 16 ; Fred, boerie, boerie, coke, 6 ; Khatija, B&E, burger, boerie, burger, B&E, coke, water, coke, coke, water, 20 ; “””

Next, we need to know what John was selling and how much each item was selling for, if we want to figure out how much money he made. For this, we’ll put the prices of the food and drink on sale into dictionary objects. Dictionaries – denoted by curly brackets “{}” – pair up two variables in a particular manner. This allows us to pair up items and their prices as key:value pairs. We can always update this dictionary if John’s prices ever change, or if he wants to add more items to his stall in future sales days!

ItemList = {"burger":35, "boerie":25, "B&E":20, "chips":20, "coffee":12, "coke":12, "water":10}

 

Get some clean data for the program

Now that we know what John’s stall sells, let’s clean up his sales info. While spaces are good for readability, we only care about the variables themselves and their values. Removing spaces will make the list easier to search and manipulate with our program. 

One way to accomplish this is writing a loop that goes through the string one character at a time and replaces or removes characters that match one we provide, but that would take more space and lines of code. We always want to look for the most efficient way to write our code, so instead we’ll be using the integrated function that Python has for this, .replace(). We can tell it what to look for, and what to put in its place, all in a single line – plus, we can also use it to remove any accidental characters we don’t want in the sales receipt, or deal with the possibility that each salesperson would write a different character (other than a semicolon, say a ‘#’) after they write down a sale.

daily_sales = daily_sales.replace(" ", "")

Now we want to take that large, unwieldy wall of text from earlier, and make it a far more manageable list of sales. Remember that a list is just a group of variables combined in a single collection through the use of square brackets.  

The sales team at the stall separated their individual sales info by using a semicolon. We can use this info in Python’s integrated .split() function, which takes a string and turns it into a list of items, splitting up the list elements according to a certain rule you provide.

all_sales = daily_sales.split(";")

This will give us a list of all individual sales transactions – but if you print this list, you’ll notice that each ‘sale’ is a string, and not a list object! That’s a problem, because we want to have each thing the sales person writes down be an item in a list we can easily examine and use. So, let’s combine another .split() function with a loop that goes through that list of strings, and turns each string into a list, splitting items wherever there is a comma (the punctuation the sales team used to separate each item they sold in a single sale.)

As a reminder, a for statement is a simple loop that allows you to do something with each element in a given collection of data. 

all_sales_split = []
tempstring = ""
for x in all_sales:
         tempstring = x.split(",")
         all_sales_split.append(tempstring)

If you print this new list, you’ll notice that it’s a list that contains lists. You’ve just created a nested list – these are powerful programming objects that you’ll use in a lot of future programs. 

 

Get each salesperson’s list of sales

Now that we have a list of individual sales lists, we can make those calculations John asked us for. Let us first create empty lists for each employee. 

FredSales = []
NoxiSales = []
KhatijaSales = []

We will fill these with information from the main list. 

Did you notice that the first item on every sale is the sales person’s name? We can use this to find which sales belong to who, and put those into their own personal sales lists using Python’s .append() method. Appending means adding a new item to the end of a list. As with the other integrated methods, it saves you time.

Remember that the Python language is zero-indexed, meaning we start counting list items from zero, and not from one (like a normal person would). So if we check to see which name is in the first element of the list – AKA index address 0 – we can find who made the sale. 

We will create a function to do this. However, to avoid having to create a separate function to find each team member’s sales, we will create a single function that receives a parameter to do its work. A parameter is a special kind of variable that serves as a kind of input for the piece of code you’re executing. Imagine you’ve got a program that paints, and you give it a jar of red paint: it will paint, using the parameter of red paint. 

Using a for statement, we will check each item in the list, using an if statement to check the name in the first list item (index 0). This if statement does something depending on a certain condition being true: here, if the first list element of each sale matches a person’s name, we add that list to their personal sales list. 

def create_salesperson_lists(listitem):
       for x in listitem:
                if x[0] == "Fred":
                         FredSales.append(x)
                elif x[0] == "Noxi":
                         NoxiSales.append(x)
                elif x[0] == "Khatija":
                         KhatijaSales.append(x)

 

Calculate the total items sold and total sales revenue

Now we need a function to count the total number of individual items sold, tally them up, and calculate total revenues from all sales. This function will use John’s dictionary of items for sale and their prices. 

We do the same thing as we did above: create a function that uses John’s dictionary object of items and prices as a parameter. Using another for statement, this function will run through the dictionary object, checking each key against our list of items sold. It then finds the price of that item using the integrated Python dictionary method .get(), which returns the value (the selling price of an item) of each key (the item for sale). For each key in John’s sales dictionary object, it gets the price of that item with .get(), counts the number of times that item appears in our sales info using the integrated .count() function, and then tallies it all up, printing the result to the user. We also keep a track of how many items we sold in total with an integer variable called TotalSales.

Remember: when you try to print something that isn’t a string, Python will display an error. To prevent this error, you have to convert that variable into a string first, by using the integrated str() function.

def total_items_sold_and_totals(list_of_all_things_on_sale):
         TotalSales = 0
         for key in list_of_all_things_on_sale:
                 price_of_item = list_of_all_things_on_sale.get(key)
                 total_items_sold = daily_sales.count(key)                 
                 food_item_subtotal = (total_items_sold*price_of_item)
                 print("Total sales for '"+key+"' were "+str(total_items_sold)+" for a total value of R"+ str(food_item_subtotal))
                 TotalSales += food_item_subtotal 
         print("The total revenue generated from all sales was R"+str(TotalSales))

Now that we have the main functions we need, let’s use them (also known as “calling the functions”), providing them the sales list and dictionary of sales items as their parameters. They’ll take those lists, and do what we told them to do with the information those lists contain. 

total_items_sold_and_totals(ItemList)
create_salesperson_lists(all_sales_split)

 

Help John see who is working hard, and how much tips to pay out

John also wants to know how hard each employee was working so let’s tell him how many sales each person made. We can do this by checking how long each sales list is, using Python’s len() function. 

print("The total number of Fred's sales is: " + str(len(FredSales)))
print("The total number of Nox's sales is: " + str(len(NoxiSales)))
print("The total number of Khatija's sales is: " + str(len(KhatijaSales)))

Finally, John wants to make sure he’s paying each person the tips they earned. We know from looking at the sales lists that the last thing each sales person wrote down was how much of a tip they made. In Python the first item in a list – as we said – is at address 0; because of something called negative indexing, the last item in that list will be at address -1. By using a negative index we can fetch the last element in a list even if we don’t know how long that list is, and tally up these values using a for statement. 

Let’s create a function that takes a salesperson’s sales list as a parameter, and check the last item of each sale, each time adding the number we find to a running tips total. 

def tip_calculator(sales_person_list):
        Salesman_tip = 0
        for x in sales_person_list:
                 tempstr = x[-1]
                 tempint = int(tempstr)
                 Salesman_tip += tempint
        return Salesman_tip

Now we can call the function we just made, using each person’s sales list as a parameter. Print the result, and you’ll find out how much they made in tips.

FredTip = tip_calculator(FredSales)
NoxiTip = tip_calculator(NoxiSales)
KhatijaTip = tip_calculator(KhatijaSales)
print("The total tips earned by Fred were R" + str(FredTip))
print("The total tips earned by Noxi were R" + str(NoxiTip))
print("The total tips earned by Khatija were R" + str(KhatijaTip))

There you have it: using just the lessons learned in HyperionDev’s free trial, we’ve taken John’s messy note/text file and turned it into usable data that he can use to manage his business. 

Of course, our free trial only teaches so much: to learn more advanced concepts and abilities – like being able to turn this simple code into an actual app John can use to manage his stall – you can sign up for the full bootcamp. Our bootcamps in web development, software engineering, and data science teach you practical skills for real-world applications like this. Best of all, you get your own personal mentor and frequent expert review of the code you write, all so you can graduate as a job-ready developer in less than 6 months. 


Find out more about our bootcamps here, and sign up for your free trial today.