Chapter 7 Activity Instructions

Activity: Vacation Planner

 

Beach Vacation IllustrationIn this chapter, you learned how to work with the dates and times using the Python datetime module, create random numbers with the random module, and perform mathematical operations using the math module. After all that hard work, it's time for a vacation!

You are in charge of creating a program that will plan out a tropical vacation. The program will ask the user for a starting date and an ending date. Then, the program will select and print a random activity for each of the days in the vacation. It will keep track of activity prices and present a summary of costs at the end.

For this program, you will continue to use earlier skills such as looping over a range, managing lists and formatting output strings. Be sure to review earlier chapters on those topics, if needed, for a reminder. The sample run below demonstrates how the program should work.

Enter the starting date of your vacation (MM/DD/YYYY): 06/01/2019
Enter the ending date of your vacation (MM/DD/YYYY): 06/08/2019
Your vacation is 7 days long
On 06/01/2019, Shopping costs $200.00
On 06/02/2019, Sleeping costs $0.00
On 06/03/2019, Snorkeling costs $10.00
On 06/04/2019, Sunbathing costs $0.00
On 06/05/2019, Sleeping costs $0.00
On 06/06/2019, Scuba Diving costs $150.00
On 06/07/2019, Helicopter Ride costs $450.00
The most expensive day cost $450.00
The least expensive day cost $0.00
Your total trip cost is $810.00
Your average cost per day is $115.71

Note: The number of days in the vacation is found by simply subtracting the dates. So, 06/08/2019 - 06/01/2019 should produce a 7-day vacation, not an 8-day vacation.

Starting Code

We are providing some starting code for your VacationPlanner program. If you are working offline, you can copy and paste it into a "VacationPlanner.py" source file that you create. But if you are working with CompuScholar's auto-graded system, you will find this code waiting for you when you click on the activity link in the course interface.

# Student name

# import modules

# tuples of daily options and matching prices
options = ("Snorkeling","Scuba Diving","Fishing","Sunbathing","Shopping","Helicopter Ride","Sleeping")
prices  = (10.00, 150.00, 25.00, 0.00, 200.00, 450.00, 0.00)

# get and parse vacation starting and ending dates






# calculate and print the timedelta difference between dates



# initialize empty costs list


# for each day of the vacation


  # pick a random activity index
  

  # read the activity description and cost
  
  

  # calculate current date and display string for that date
  
  

  # print daily details and append cost to end of costs list
  print(str.format("On {}, {} costs ${:.2f}",thisDateString,activity,cost))
  

# print most and least expensive days



# calculate and print total cost of the trip



# calculate and print the average cost per day




Detailed Program Requirements

Write your program by carefully following the steps listed below. For the best grade, please match the expected output exactly (including correct spelling and case of all messages). The starting code will take care of some basic tasks for you. It also contains some comments to help keep you on track.

There are quite a few steps in this program, but you can do it! Just take one step at a time; each step will rely on a skill you have learned in this or earlier chapters. Test your code at the suggested checkpoints (or more frequently) to make sure it works before continuing to the next section.
  1. Start with a comment that contains your student name.
  2. Import the datetimemath and random modules (you will need to use all three).
  3. Review the tuple named options that contains a list of possible daily activities (we provide this line for you).
  4. Review the tuple named prices that contains a list of costs for each daily activity (we provide this line for you).

Note that values in the options and prices tuples are arranged so matching activities and costs will always have the same numeric index value. This means if you use a specific index like 2 to access an activity in the options list, you can use that same index to get the cost for that activity from the prices list.

  1. Next, prompt the user to enter a starting date and store the resulting string in a variable named startDateString. The input() prompt should show the user the expected date format, which is MM/DD/YYYY. Here is an example prompt: "Enter the starting date of your vacation (MM/DD/YYYY): "
  2. Use the datetime.datetime.strptime() function and convert the startDateString to a datetime object in a variable named startDate. You will need to create a format string that correctly parses input in the form "MM/DD/YYYY".
  3. Repeat the above two steps to prompt the user for a stop date and store the results in stopDateString. Then, use the strptime() function again to parse that input and store the restulting datetime object in a variable named stopDate.
  4. Next, create a variable named delta that holds a timedelta object, created by subtracting the stopDate from the startDatedelta will hold the length of time in the vacation, and you can later read the number of days as "delta.days".
  5. Next, print a message to the screen in the format "Your vacation is {} days long". Use str.format() to create the message and delta.days to provide the data.
Checkpoint: Go ahead and run your program now to confirm it works as expected up to this point. Your user's date inputs should be correctly parsed without any run-time exceptions, and you should see the number of days correctly displayed. You will get a run-time error afterwards because you have not yet completed later parts of the starter code - won't worry about that!
Enter the starting date of your vacation (MM/DD/YYYY): 06/01/2019
Enter the ending date of your vacation (MM/DD/YYYY): 06/03/2019
Your vacation is 2 days long
Traceback (most recent call last):
  File "code1.py", line 40, in 
    print(str.format("On {}, {} costs ${:.2f}",thisDateString,activity,cost))
NameError: name 'thisDateString' is not defined

Once your user's input is correctly understood and the number of days is known, you will move to a "for" loop that will iterate from 0 up to the number of days. Within the loop, daily activities will be randomly selected and displayed, and you will keep a running list of prices in the costs list.

  1. Create an empty list called costs.
  2. Create a "for" loop with an index variable named "i" that will iterate from 0 up through delta.days (remember to use the "range" feature to do this). Inside the loop, take the following actions:
    1. Create a variable called activityIndex and set it equal to a random integer between 0 and len(options), which is the length of the options list. There are 7 options in the list, so your random index should be somewhere from 0 up through 6, but not including 7. Use the random.randrange() function for this step.
    2. Create a variable called activity and set it equal to the element in the options tuple at the activityIndex.
    3. Create a variable called cost and set it equal to the element in the prices tuple at the activityIndex.
    4. Create a variable called thisDate, which will hold the date of the current day within the vacation. Calculate this date by setting thisDate equal to startDate plus a timedelta object. The timedelta object should be created with a number of days equal to the loop index variable "i" (e.g. thisDate = startDate + datetime.timedelta(days=i)).
    5. Create a variable named thisDateString and set it equal to a string formed from datetime.datetime.strftime(). You want to take the thisDate object and convert it to a string in the format "MM/DD/YYYY", just like the input dates.
    6. The next line (provided for you) will print a summary of the daily activity, including the thisDateStringactivity and cost variables that you created above.
    7. Finally, within the loop, append the cost to the costs list. You'll do some math on all these costs later.
Checkpoint: When finished with your "for" loop, run your program again to test everything to this point. In addition to getting the user input and displaying the number of days, your program should now display the daily activities and costs with a nicely formatted date string for each day.
Enter the starting date of your vacation (MM/DD/YYYY): 06/01/2019
Enter the ending date of your vacation (MM/DD/YYYY): 06/03/2019
Your vacation is 2 days long
On 06/01/2019, Helicopter Ride costs $450.00
On 06/02/2019, Shopping costs $200.00

Finally, after the "for" loop, your program will provide 4 pieces of information based on the activity costs you tracked in the costs list. The program will show the most expensive day, the least expensive day, the total trip costs and the average cost per day. These things are easily calculated using math and core library functions. Note that all cost outputs should be formatted with two decimal digits, as in "$10.50".

  1. Find and print the most expensive day using str.format() with the message "The most expensive day cost $<X>". Select the right placeholder format {} for <X> to show two decimal digits (Hint: look at code we already provided for you). Use a core library function to find the maximum value in the costs list.
  2. Find and print the least expensive day using str.format() with the message "The least expensive day cost $<X>". This is very similar to the previous step.
  3. Create a variable called total that holds a sum of all the values in the costs list. Use a math module function for this step!
  4. Print the total cost with the message "Your total trip cost is $<X>". You are getting pretty good at using str.format() by now, right?
  5. Create a variable called average that divides the total by the number of days in the vacation. You have used the number of days a couple of times already, so you know how to read that value.
  6. Print the average cost with the message "Your average cost per day is $<X>".
Checkpoint: Your program is now complete, and you should be able to run it several times with different input dates. Verify that the program randomly selects an activity for each day and correctly calculates the min, max, total and average costs. Check out some of the sample runs in the Activity Results section for comparison. If you select dates with just a few days between them, your program output will be easier to verify!

 

Activity Results

The following example runs demonstrate some possible input and output combinations. Each run will select random activities, so your output will not match ours exactly, even if you enter identical dates. But you should be able to verify that the correct number of days and dates are displayed and that the mathematical operations are carried out correctly.

Test #1 - Start Date: 06/01/2019, End Date: 06/03/2019

Enter the starting date of your vacation (MM/DD/YYYY): 06/01/2019
Enter the ending date of your vacation (MM/DD/YYYY): 06/03/2019
Your vacation is 2 days long
On 06/01/2019, Helicopter Ride costs $450.00
On 06/02/2019, Sleeping costs $0.00
The most expensive day cost $450.00
The least expensive day cost $0.00
Your total trip cost is $450.00
Your average cost per day is $225.00

Test #2 - Start Date: 07/03/2020, End Date: 07/10/2020

Enter the starting date of your vacation (MM/DD/YYYY): 07/03/2020
Enter the ending date of your vacation (MM/DD/YYYY): 07/10/2020
Your vacation is 7 days long
On 07/03/2020, Scuba Diving costs $150.00
On 07/04/2020, Scuba Diving costs $150.00
On 07/05/2020, Sleeping costs $0.00
On 07/06/2020, Fishing costs $25.00
On 07/07/2020, Scuba Diving costs $150.00
On 07/08/2020, Snorkeling costs $10.00
On 07/09/2020, Sleeping costs $0.00
The most expensive day cost $150.00
The least expensive day cost $0.00
Your total trip cost is $485.00
Your average cost per day is $69.29

When your program is complete and tested, submit it for grading!

Activity Rubric

If your project is being automatically graded by our system, your grade will be calculated from 0 to 100 as follows:

PointsDescription
3All three modules (datetimemathrandom) imported and used
5strptime() used to parse user's input dates
5delta object initialized correctly
6"for" loop is created with correct index variable and range
5activityIndex defined and initialized with random integer in correct range
5activity defined and initialized correctly
5cost defined and initialized correctly
5thisDate defined and initialized correctly
5strftime() used to format daily dates within "for" loop
6min() and max() functions used to get least and most expensive prices
5math.fsum() function used as expected to get total trip cost
5average defined and initialized correctly
10Starting and ending date prompts displayed with format "MM/DD/YYYY" instructions
10Message about number of days printed to screen
10One message per vacation date is printed with correct date, activity details and costs
10Most expensive, least expensive, total trip cost and average daily cost messages printed at the end

Last modified: Sunday, 18 August 2019, 9:40 PM