- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Sales Tax Calculator in Python
Build a reusable Python sales tax calculator that starts with shell-style tax math and turns it into a saved script. Along the way, you will implement core math helpers, readable printed output, basic text input parsing, and a simple main entry point that can run in several modes.
Lab Info
Table of Contents
-
Challenge
Step 1: Explore the Project and the Learning Path
This lab turns a few lines of Python shell math into a small, reusable sales tax calculator program. The shell is great for quick experiments because it echoes values automatically. Scripts are what you keep, rerun, and share.
To stay organized, this project splits responsibilities across a few files:
app/calculator.py— defaults and tax mathapp/formatting.py— currency and summary outputapp/io_helpers.py— input parsing and promptsapp/workflows.py— demo workflowtax.py— script entry point
The starter code includes tests for each task. Work through the tasks in order and run the check when you finish one. > If you get stuck, you can refer to the provided solution code for each task, available in the
solutionfolder. info> This lab experience was developed by the Pluralsight team using an internally developed AI tool. All sections were verified by human experts for accuracy prior to publications. However, content may still contain errors or inaccuracies, and we recommend independent verification.
To report a problem or provide feedback, click here. Feedback may be used to improve accuracy in accordance with our Privacy Policy. -
Challenge
Step 2: Recreate the Shell Math in Reusable Calculator Functions
This step builds the mathematical core in
app/calculator.py. In the shell, one expression likeamount + amount * taxis enough. In a script, it helps to name the pieces: defaults at the module level, a helper for tax only, and a helper for the full total. That separation makes the code easier to test and reuse. A common starting point isamount = 10andtax = 0.06. In a script, those values belong in one clear place so the rest of the code can reuse them. Python often uses uppercase names such asDEFAULT_AMOUNTto signal stable default values. Next, pull the tax calculation into its own helper. The expressionamount * taxbecomes a function that returns only the tax portion, not the full total. The full formula istotal = amount + amount * tax. Instead of repeating the multiplication, callcalculate_tax()fromcalculate_total()so each function has one clear job. -
Challenge
Step 3: Turn Raw Numbers Into Readable Script Output
The shell shows a value when you type a variable name, but a saved script does not display anything unless you print it. This step adds formatting helpers and connects them to the script. Raw floats like
10.6are not ideal for users. An f-string with a.2fformat specifier can turn that into currency text such as$10.60. A single formatted number is not enough for a useful script. Build a multi-line summary that explains the amount, tax rate, and total, then return that string so other parts of the app can print or test it. Now connect the default calculator values to visible output. Build the summary first, then pass it tooutput_funcso the script produces console output when it runs. -
Challenge
Step 4: Accept New Values Through Text Input
Changing values still means editing code unless the program can read input. User input arrives as text, so you need parsing and validation before the calculator can use the values. Start with the purchase amount. Strip whitespace, convert with
float(), and reject invalid or negative values withValueError. Tax rates are trickier because users may enter0.06,7, or7%. Normalize those forms into one decimal rate the calculator can use, such as0.07. With the parsers in place, wire up a small interactive workflow: prompt for both values, parse them, build the summary, and print the result. -
Challenge
Step 5: Finish Reusable Workflows and the Script Entry Point
The final step adds application flow. A demo workflow can show both example calculations in sequence, and
main()gives the script one clear entry point. Start with one default calculation and then a rerun with new values. Package both examples into one reusable demo function instead of editing variables each time. Finish by routing execution throughmain(). Theif __name__ == '__main__':guard keeps imports safe, whilemain()decides whether to run the default, demo, or prompt workflow. Runpython tax.pyto see the finished calculator. You now have reusable math helpers, readable output, input parsing, and a clean script entry point.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.