W01 Code-along Activity: Discount
Code-along Activity Overview
For this activity, you will code along with an instructor video that walks you through each step of the activity below. Make sure that you pause the video, or watch sections over if you need to, so that you are able to complete the entire activity.
Your instructor will post a link to the video in Microsoft Teams.
Background
You work for a retail store that wants to increase sales on Tuesday and Wednesday, which are the store’s slowest sales days. On Tuesday and Wednesday, if a customer’s subtotal is $50 or greater, the store will discount the customer’s subtotal by 10%.
Program
Work along with your instructor to write a Python program that gets a customer’s subtotal as input and gets the current day of the week from your computer’s operating system. Your program must not ask the user to enter the day of the week. Instead, it must get the day of the week from your computer’s operating system.
If the subtotal is $50 or greater and today is Tuesday or Wednesday, your program must subtract 10% from the subtotal. Your program must then compute the total amount due by adding sales tax of 6% to the subtotal. Your program must print the discount amount if applicable, the sales tax amount, and the total amount due.
Requirements
-
Your program asks the user for the subtotal but does not ask the user for the day of the week. Your program gets the day of the week from your computer’s operating system.
-
Your program correctly computes and prints the discount amount if applicable.
-
Your program correctly computes and prints the sales tax amount and the total amount due.
Enhancements
-
Add code to your program that the computer will execute if today is Tuesday or Wednesday and the customer is not purchasing enough to receive the discount. This added code should compute and print the difference between $50 and the subtotal which is the additional amount the customer would need to purchase in order to receive the discount.
-
Near the beginning of your program replace the code that asks the user for the subtotal with a loop that repeatedly asks the user for a price and a quantity and computes the subtotal. This loop should repeat until the user enters "0" for the quantity.
Instructions
- Create a folder for this week's code-along program.
- Open the folder you just created in VSCode.
- Create a new file named discount.py.
- Find the video that your instructor has posted for this assignment and code along with them to complete each of the requirements. Work through the requirements in order with the instructor rather than jumping ahead to more complicated steps to ensure you do not miss any concepts.
Alternative Video Link and Transcript
In addition to the link that your instructor posted, you are also welcome to watch the following code walkthrough or use its transcript:
Additional Resources
You may use these additional resources for further study.
Sample Solution
In addition to the instructor video, you can also view this solution. (This solution may be slightly different than the final code that you and your instructor created.)
Helpful Documentation
-
The preparation content for this week01 explains how to call a function and a method.
-
The
datetime.now()
method from the standard Pythondatetime
module will get the current date and time from your computer’s operating system. Here is an excerpt from the official reference for thedatetime.now
method:-
datetime.now(tz=None)
-
Return the current local date and time.
tz is optional, but if it is not
None
, it must be atzinfo
(time zone information) object
The
weekday()
method will get an integer that represents the day of the week from adatetime
object. Here is an excerpt from the official documentation for theweekday
method:-
dt.weekday()
- Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
The following Python code imports the
datetime
class from thedatetime
module, calls thedatetime.now
method to get the current date and time from a computer’s operating system, and then calls theweekday
method to get the day of the week as an integer.# Import the datetime class from the datetime # module so that it can be used in this program. from datetime import datetime # Call the now() method to get the current # date and time as a datetime object from # the computer's operating system. current_date_and_time = datetime.now() # Call the weekday() method to get the day of the # week from the current_date_and_time object. day_of_week = current_date_and_time.weekday() # Print the day of the week for the user to see. print(day_of_week)
> python day_of_week.py 4
After the computer executes line 7 in the above code, the current_date_and_time variable will hold the current date and time. After the computer executes line 10, the day_of_week variable will hold 0 if today is Monday, 1 if today is Tuesday, and so on to 6 if today is Sunday.
-
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
-
If today is any day except Tuesday or Wednesday, run your program using the inputs shown below. Ensure that your program’s output matches the output shown below.
> python discount.py Please enter the subtotal: 42.75 Sales tax amount: 2.56 Total: 45.31 > python discount.py Please enter the subtotal: 55.20 Sales tax amount: 3.31 Total: 58.51
-
If today is Tuesday or Wednesday, run your program using the input shown below. Ensure that your program’s output matches output shown below.
> python discount.py Please enter the subtotal: 42.75 Sales tax amount: 2.56 Total: 45.31 > python discount.py Please enter the subtotal: 55.20 Discount amount: 5.52 Sales tax amount: 2.98 Total: 52.66
-
Is there a simple way to test your program for all the days of the week without testing on seven consecutive days and without changing your computer’s operating system date? Hint: In your program immediately after these three lines of code:
# Call the weekday() method to get the day of the # week from the current_date_and_time object. day_of_week = current_date_and_time.weekday()
day_of_week = 2
Submission
Please pause and re-watch any sections of the instructor video necessary until you have completed the entire activity and your program runs the same as the instructor's. When you are finished:
- Return to Canvas to take the quiz.
Useful Links:
- Return to: Week Overview | Course Home