COMP 141: Project 4
Polly's Pawfully Purrfect Pets
Polly runs a pet-sitting business called Polly's Pawfully Purrfect Pets.
People hire her to take care of their dogs and cats while they are out of town.
In this program, you will use functions and loops to calculate information about
Polly's prices for the pet-sitting itself, and for the cost of food.
Pet Sitting Prices
For
each customer, Polly calculates their daily rate based on the number of dogs and cats the
customer owns, the distance she has to
drive to get to the customer's house, and the level of care the customer chooses.
Polly offers three levels of service: basic, pro, and supreme. For dogs, basic care is
$12/day, pro care is $15/day, and supreme care is $19/day. For cats, the rates are the
same, except $1 less per day. These rates are per-pet, so for two dogs, double the dog
rate, and so on, for multiple dogs and/or cats. (Polly does offer discounts for
multiple pets, described below.)
Polly has to drive to each customer's house, so she adds an extra fee onto her daily
rates if she has to drive a long distance. If the customer lives more than two miles away,
she adds one dollar per mile (over two) to the daily rate. So if a customer lives 1 or 2
miles away, there is no extra fee; for three miles, add $1/day; for four miles, add $2/day,
and so on.
Polly offers discounts for taking care of multiple pets. The discount is applied
after multiplying her base rates by the number of dogs and cats she's caring for.
(For instance, the base rate for taking care of 2 dogs and 2 cats for a customer who
lives 5 miles away who chooses the "basic" level of care is: 2 * $12 (for the dogs) +
2 * $11 (for the cats) + $3 (for the extra driving) = $24 + $22 + $3 = $49/day.)
If Polly cares for one pet, there is no discount. For taking care of two pets (any combination
of dogs and cats), she takes 15% off the total daily rate. For three pets, she takes 25%
off, and for four or more pets, she takes off 35%.
Once Polly determines the base rate, she multiplies the base rate by the number of
days she's caring for the pets to get the total price. If she's taking care of the pets
for a week (7 days) or more, then she applies one last 10% discount to the total price.
Food prices
Among other things, Polly takes care of feeding the pets while the customer is away.
She pays for the food herself, so she wants to compute its cost ahead of time.
For dogs, she can buy large bags of food for $13 each that feed one dog for one week (7 days),
or small individual bags of food that feed one dog for one day for $2 each.
For dogs, she can buy large bags of food for $10 each that feed one cat for one week (7 days),
or small individual cans of food that feed one cat for one day for $1.50 each.
What you need to do
Step 0: Refresh your memory on commenting your code
This is the first time you'll be using function you write yourself, so make sure you understand
how to comment on them before the "def" line for each function.
Step 0.5: Copy the starter Python code below into a new file
Step 1: Write the following functions
You must write the following functions. After writing each function, test them from
the Python shell. You do this by running the program (press F5 or choose Run -> Run Module)
and calling the function from the Python shell with different arguments and seeing if
you get the right answers.
- daily_rate(num_dogs, num_cats, service_level, distance): This function
returns Polly's daily rate based on the number of dogs, number of cats, the service
level (basic/pro/supreme), and the distance Polly has to drive.
Here are some examples for testing the function:
- 1 dog, 0 cats, basic level, 1 mile away:
daily_rate(1, 0, "basic", 1) should return 12
- 1 dog, 1 cat, pro level, 3 miles away:
daily_rate(1, 1, "pro", 3) should return 25.5
- 2 dogs, 2 cats, supreme level, 5 miles away:
daily_rate(2, 2, "supreme", 5) should return 50.05
Once this function is working, use it to write the next one:
- total_price(num_dogs, num_cats, service_level, distance, num_days): This function
returns Polly's total fee charged based on the number of dogs, number of cats, the service
level (basic/pro/supreme), the distance Polly has to drive, and the number of days the
customer will be away.
Here are some examples for testing the function:
- 1 dog, 0 cats, basic level, 1 mile away, 3 days:
total_price(1, 0, "basic", 1, 3) should return 36
- 1 dog, 1 cat, pro level, 3 miles away:
total_price(1, 1, "pro", 3, 7) should return 160.65
- 2 dogs, 2 cats, supreme level, 5 miles away:
total_price(2, 2, "supreme", 5, 10) should return 450.45
The total_price function should call the daily_rate function. Do not duplicate the functionality
of daily_rate a second time.
- dog_food_price(num_dogs, num_days): This function returns the total dog food
price that a certain number of dogs will eat over a certain number of days. This must
be computed exactly to the day; there must not be any extra food bought. Furthermore,
remember that if there are 2 dogs being cared for over 4 days, you need 8 days worth of
food (so you can buy one big bag and one individual bag)
Helpful hints: There are two operators in Python that will help you here. The double-slash
operator (//) does "integer division", which means the resulting quotient discards any
fractional portion, whereas the percent sign operator (%) returns
remainders. Example:
x = 14 / 3 # x is now 4.666... (regular division)
y = 14 // 3 # y is now 4 (the integer quotient when 14 is divided by 3 is 4)
z = 14 % 3 # z is now 2 (the remainder when 14 is divided by is 2)
Here are some examples for testing the function:
- 1 dog, 3 days:
dog_food_price(1, 3) should return 6
- 2 dog, 4 days:
dog_food_price(2, 4) should return 15
- cat_food_price(num_cats, num_days): This function returns the total cat food
price that a certain number of cats will eat over a certain number of days.
- difference_of_dates(month1, date1, month2, date2): This function returns
the number of days between the two dates specified as arguments. The month arguments
are given as integers from 1-12 (1=January, 2=February, etc). I've written
two functions in the starter code to help you with this---you should call day_of_year(month, date)
inside the body of difference_of_dates and your code will be very simple if you do this.
day_of_year(month, date) returns the day of the year a certain month and day falls on. For instance,
January 1 is the 1st day of the year, and December 31st is the 365th day.
Here are some examples for testing the function:
-
difference_of_dates(1, 12, 1, 14) should return 2 (2 days from Jan 12 to Jan 14)
-
difference_of_dates(7, 29, 12, 6) should return 130 (130 days from July 29 to Dec 6)
Once you have written these functions, tested them, and they all work, you can move on
to Step 2, which is writing the main function.
Step 2: Write main()
Write a main function to do the following:
- Ask the user for four integers: the month & day they are leaving, and the month & day
they are returning.
- Compute the length of time they will be gone in days (call difference_of_dates) and
print it out.
- Ask the user for three integers: the number of dogs they own, the number of cats,
and the number of miles the sitter has to travel.
- Compute the cost for the three levels of service (basic, pro, supreme), by calling
total_price three times. Print out the three values.
- Call dog_food_price and cat_food_price to compute the amount of dog & cat food
that will need to be purchased. Print out the individual prices and the total cost of the food.
Step 3: Add a loop to main
Add a while loop to your main function so that after the total cost of food is printed,
the user is asked if they want to calculate the costs for a different pet sitting situation.
If they answer yes, they can do it again, otherwise the program ends.
Start with this code
You should use the following program structure: (Yes, literally copy it into a blank
Python program)
# Name:
# Date:
# Class:
# Project:
# Pledge:
# Description:
# This function calculates the day of the year for the "zero-th" day of each month.
# month: the number of the month from 1-12.
# returns: the day of the year
def day_of_year_for_month(month):
if month == 1:
return 0
elif month == 2:
return 31
elif month == 3:
return 31 + 28
elif month == 4:
return 31 + 28 + 31
elif month == 5:
return 31 + 28 + 31 + 30
elif month == 6:
return 31 + 28 + 31 + 30 + 31
elif month == 7:
return 31 + 28 + 31 + 30 + 31 + 30
elif month == 8:
return 31 + 28 + 31 + 30 + 31 + 30 + 31
elif month == 9:
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31
elif month == 10:
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30
elif month == 11:
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
elif month == 12:
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
# This function calculates the day of the year for any day of each month.
# month: the number of the month from 1-12.
# date: the day of the month from 1-31.
# returns: the day of the year
def day_of_year(month, date):
return day_of_year_for_month(month) + date
Example run
What month are you leaving? 4
What day of the month are you leaving? 5
What month are you returning? 4
What day of the month are you returning? 6
You will be gone for 1 days.
How many dogs do you own? 1
How many cats do you own? 1
How many miles will the sitter have to travel? 4
The basic level will cost you: 21.25
The pro level will cost you: 26.349999999999998
The supreme level will cost you: 33.15
Dog food will cost you: 2
Cat food will cost you: 1.5
Total cost of food is: 3.5
Do you want to calculate costs for another situation? yes
What month are you leaving? 6
What day of the month are you leaving? 7
What month are you returning? 7
What day of the month are you returning? 15
You will be gone for 38 days.
How many dogs do you own? 2
How many cats do you own? 3
How many miles will the sitter have to travel? 5
The basic level will cost you: 1333.8
The pro level will cost you: 1667.25
The supreme level will cost you: 2111.85
Dog food will cost you: 142
Cat food will cost you: 163.0
Total cost of food is: 305.0
Do you want to calculate costs for another situation? no
Thanks for using the calculator!
Submission
- Your program should be named pets_yourLastName_yourFirstName.py.
- Submit your Python file on Moodle under Project 4.