While going through my Python course, one of my coding challenges was simple but fun from a retail point of view as I used to be a cashier long ago as a teenager and seeing it gave me insight in how the programming in retail software and accounting software might look like.
The challenge was is as follows:
- Build 2 functions for applying discounts
- One function is for a student discount of 10%
- The other function is for applying a regular shopper’s discount of 15%
- We must be able to apply both discounts to the product if necessary.
So once we get one function the other is similar so lets focus on the Student Discount Function.
We want to subtract 10% from the price of a product so we need to start with the formula. If you’re someone who likes to plan things down to the penny with taxes and sales going on, this isn’t terrible hard. x-(x*.10) is what we need to use in our function. So creating the function, it should look like so:
From there, we can easily create our regulars function for Regular Shoppers by replacing .10 with .15 as such:
Now we need a price that we want to use to try out our functions on. Let’s say 150 for a Zojirushi Water Boiler (because hot water on demand and at the temperature you want is something students and anyone else could use.
Now using functional programming, we want to be able to apply either function OR both simultaneously. How do we write it? Well, we call on our function and use zojirushi as our argument.
When we run the code, this is what we get(not including tax aha):
It is a simple challenge to solidify how to do functional programming in python but the prompt itself also shows us what is going on in those registers, what the bare bones of retail software can do and does do for us.
The whole code: