Chapter 2 Activity Instructions
Chapter Two: Working with Data
Activity: Cash Register
In this chapter, you learned about data types and variables that hold string, integer and floating-point values. You also learned how to glue strings together with concatenation and perform simple numeric addition, subtraction, multiplication and division. For this project, you are going to put all those skills to use with a simple Cash Registerprogram.
Reminder - Your Coding Platform
In the first chapter activity, you learned how to use our online Python engine to submit projects for grading. Or, you may have followed other instructions to code and test Python programs on your own computer and submit them to your teacher separately. Please review the first chapter activity instructions and your choice of a coding platform, if needed, for details on how to complete and submit this project. Your project might be auto-graded by our system, or it may be graded by your teacher.
Background - Calculating A Bill of Sale
When you go to the store and purchase some items, the total amount of your purchase is calculated at the cash register when you check out. Do you know exactly how your total payment is calculated? There are three basic pieces of information that you need to know:
- How many items are you purchasing?
- What is the cost of each item?
- What is the tax rate?
Let's assume that you only buy one kind of item, like a shirt, and the cost for each shirt is the same. As you might guess, these pieces of information can be stored in Python variables. An integer variable will nicely hold the number of items you want to buy, and a floating-point variable will store the price per item.
numItems = 3 costPerItem = 12.50
Now, your sub-total is the price you are going to pay before taxes are added. The sub-total is calculated by multiplying together the number of items and the cost per item.
subTotal = numItems * costPerItem
Great, now the last step is to calculate and add tax to the sub-total. A tax rate is expressed as a percentage like 6% or a decimal like 0.06.
taxRate = 0.06
The tax rate is multiplied by the sub-total and that extra tax amount is then added to the sub-total to find your total sales price.
taxAmount = subTotal * taxRate totalPrice = subTotal + taxAmount
Now that you understand the math behind a sale, it's time to demonstrate your skills with a cash register program!
In this activity, you are going to create a Python program that will calculate and print a sales receipt from a cash register. Your receipt will show the number of items purchased, the cost per item, the sub-total, the tax rate, the tax amount and the total sales price.
Create your Python program by completing the following steps:
- Start your Python code with a comment at the top showing your name
- Declare and initialize Python variables as follows:
- numItems should hold the value 4
- costPerItem should hold the value $10.00
- taxRate should hold the value 0.08
- Next, add a statement to calculate and store the sub-total in a variable named subTotal
- Next, add a statement to calculate the amount of tax and store the result in a variable called taxAmount
- Then, add a statement to calculate the total price and store the amount in a variable called totalPrice
Great, you are now done with the math and need to show the full receipt to the screen. We want the receipt to be formatted as follows:
SALES RECEIPT Number of items : 4 Cost per item : $10.0 Tax rate : 0.08 Tax amount : $3.2 TOTAL SALE PRICE: $43.2
Remember that you can concatenate (join) together strings and numbers by using the plus sign (+) and the str() function to convert a number to a string. So, for example, you could print() the second output line with this statement:
print("Number of items : " + str(numItems))
- Add 6 print() statements to display the 6 lines in this receipt. You want to match the output lines demonstrated above.
- Remember to use the str() function when needed to convert numbers to strings.
- Remember to add the currency symbol (like $) to the front of numbers that are dollar amounts.
When you are done, run your code and verify that your cash receipt is printed to the screen. Are all the calculated amounts correct? Then, change each of the first three numeric variables (numItems, costPerItem and taxRate) and re-run your program. You should see each output line changes to reflect the new data and calculated amounts.
Here are two more example outputs that use some different initial values in the first three numeric variables.
SALES RECEIPT Number of items : 5 Cost per item : $5.5 Tax rate : 0.06 Tax amount : $1.65 TOTAL SALE PRICE: $29.15
SALES RECEIPT Number of items : 100 Cost per item : $3.75 Tax rate : 0.09 Tax amount : $33.75 TOTAL SALE PRICE: $408.75
If your project is being automatically graded by our system, your grade will be calculated from 0 to 100 as follows:
|10||Add comment with name as first line of code|
|10||Declare and use numItems variable|
|10||Declare and use costPerItem variable|
|10||Declare and use taxRate variable|
|10||Declare and use subTotal variable|
|10||Declare and use taxAmount variable|
|10||Declare and use totalPrice variable|
|10||Use addition where needed to calculate results|
|10||Use multiplication where needed to calculate results|
|10||Program produces the correct output|
Remember to use the exact variable names described in the activity instructions and format your output exactly as shown above. Case-sensitivity is important!
End of Activity