Description
Objectives:
– Understand how to implement a simple decision using an if statement.
– Understand how to implement a two-way decision using an if/else statement.
– Understand how to implement a multi-way decision using an if/elif/else statement.
– Understand the concept of boolean expressions and the boolean data types.
– Be able to read, write, and implement algorithms that employ decision structures, including
those that employ sequences of decisions and nested decision structures.
Turn in:
– hw4_lastname_code.ipynb
You should turn this file in on Canvas under the “Homework 4” link. Complete the following
checklist before turning in your code:
○ all cells in your file run without errors after restarting the kernel
○ you have no global variables (i.e., all your variables are inside functions)
○ your code is nicely commented, and any functions have docstrings
○ you have appropriately named the ipynb file
Instructions:
For this homework, you will be creating functions that will (1) ask questions to collect
data and (2) use conditionals to output personalized ads using conditionals. Each
problem will present a short scenario and task describing what type of personalized ad
should be served.
There will be a few guidelines for the first few questions and ads
you’ll be implementing, and then you will have the freedom to ask your own questions
and develop your own ads. Please read the instructions carefully and attend office
hours if you have any questions.
Your job is to write a program that decides on an ad to serve to a person on a social media
platform. The personalized ad program will prompt the user for information and then return text
that describes ads based on their inputs. Feel free to use as much (or as little) creativity as you
want for the descriptions of ads for your program.
Make sure to use the provided starter notebook. You will receive a significant deduction if you
do not use the starter notebook.
Example output is located at the end of this write-up.
Next, work on asking questions and creating functions to deliver ads based on the following
scenarios:
Problem 1 (7 points)
Scenario: For advertisers using the lowest tier of personalization on this social media platform,
there is limited data about users available—we only know whether a user self-reported that they
own a dog.
Task 1: Find out whether a user has a dog.
Using the built-in input function, ask the user if they
have a dog. Our user will more than likely type a “yes” or “no”, which we want to convert to bool
(True or False). Implement a function as described here:
● owns_dog: this function should take no parameters. It should prompt the user to
indicate whether they own a dog, and return the user’s answer as a bool.
○ Note: this function should be case insensitive. If the user answers “yes”, “YES”,
“yES”, “yes”, or any other variations of “yes”, your function should return True. If
they answer anything that is not “yes” (case insensitive), your function should
return False. Make use of string functions, rather than listing all possibilities, to
accomplish this.
Task 2: Given a bool returned by owns_dog, write a function as described here:
● serve_ad_based_on_dog: this function should take one argument (a bool): whether or
not the user owns a dog
● If the value of this argument is True, print text advertising dog food
○ Your text must start with “Dog Food Ad: ”
● Otherwise, print text advertising anything else.
○ Your text must start with “Ad: “.
For example, given that the user owns a dog, your function might print something like:
Dog Food Ad: Try our all new super delicious dog food! Your dog will love it! WOOF!
Otherwise, your function might something like:
Ad: Dirty mouth? Clean it up with new Raspberry Mint sugar free gum. For a good clean feeling,
no matter what!
Problem 2 (10 points)
Scenario: By combining data from a marketing platform with the results from personality
tests that users have completed, we know that people with at least 500 Facebook
friends are likely to be extroverts, and people with fewer than 500 friends are likely to be
introverts.
Based on analysis of user-uploaded photos, we also know that people who
are extroverts are more likely to have many photos of dogs, and introverts are more
likely to have many photos of cats. Market research has shown that dog people are
highly likely to be positively influenced by advertisements for any product that includes
dogs, and similar for cat people and cats.
Task: Find out how many friends a user has, and write a function that:
● if the user has at least (more than or equal to) 500 friends, print ad text that
includes a dog. Your text must start with “Ad with Dog: ”.
● if the user has fewer than 500 friends, print ad text that includes a cat. Your text
must start with “Ad with Cat: ”.
To solve this task, write the following functions:
● int_question: this function should take one argument, the str question to pose to the
user. It should return the user’s answer as an int. The user should not be able to “break”
your code by entering something that is nonsense, or not an int.
Specifically If the user
provides an answer that is not an integer greater than or equal to 0 (hint: see the
str.isdecimal() function), this function should print an error message, then return the
number 0. Make sure to call this function whenever you ask the user a question with an
integer response (even in problem 3).
● serve_ad_based_on_friends: a function that takes one parameter, the integer number
of friends that the user has, and prints the appropriate ad (as described above)
Problem 3 (8 points)
Scenario: Based on ad clicks and third-party data shared through purchases on a
“marketplace” platform, we have data about likely income averages for every zip code in
the country. We also know, based on self-reported age, how incomes vary based on life
stage (e.g., college students have less disposable income no matter where they live).
Advertisers strategically advertise to people who are more likely to be able to afford
their product.
Task: Given a user’s zip code and age as integers, write a function that:
● if the zipcode is in Boulder (i.e. between and including 80301 and 80310) and the
user is at least 25, print ad text for an expensive product. Your text must start
with “Expensive Product Ad: ”.
● if the zipcode is in Boulder and the user is younger than 25, print ad text for an
inexpensive product. Your text must start with “Cheap Product Ad: ”.
● Otherwise, print ad text encouraging the user to visit Boulder. Your text must start
with “Tourist Ad: ”.
Write the following function:
● serve_ad_based_on_zip_age: a function that takes two arguments, the user’s zip code
and their age, and prints an appropriate ad.
For example, given zip code = 53566 and age = 25, your function might print:
Tourist Ad: Come visit Boulder, where everything is bolder.
Problem 4 – Completing Your Program (10 points)
Write a main() function that prompts the user for which level of ad personalization they
would like to see (on a scale from 1-3, where 1 corresponds to Problem 1, 2
corresponds to Problem 2 and 3 corresponds to Problem 3).
Then, ask the user to input
the needed information for those ads (remember to use your int_question function from
earlier to get int inputs – but you may still use regular old input to get string inputs).
Finally, serve (print) the appropriate ad for the user.
See sample output below.
Comments & Style (5 points)
Your code must be commented. You must include a file comment (comment at the top with your
name, homework description, etc), inline comments, and function docstrings.
Your code should be structured so that there are no global variables, just functions. The only
code that you should have outside a function is a single call to main. Here is an example
template for your code:
def serve_ad_based_on_dog(has_dog):
“””
Docstring
“””
# Code for function
# All other functions, as described in the homework, should also be
defined.
def main():
“””
Docstring
“””
# Get input about ad personalization level
# Ask appropriate questions (to get relevant user info based on
personalization level)
# Call appropriate serve_ad function
main()
Example Output (User input in bold underlined)
Replace all places with [Your text here] with an ad that you’ve written yourself
How personalized should the ad be? (1 – 3) 1
Do you own a dog? YeS
Dog Food Ad: [Your text here]
How personalized should the ad be? (1 – 3) 1
Do you own a dog? no
Ad: [Your text here]
How personalized should the ad be? (1 – 3) 2
How many friends do you have? 500000
Ad with Dog: [Your text here]
How personalized should the ad be? (1 – 3) 2
How many friends do you have? so many
You didn’t enter a number: converting response to 0
Ad with Cat: [Your text here]
How personalized should the ad be? (1 – 3) 3
What zipcode do you live in? 80309
How old are you? 52
Expensive Product Ad: [Your text here]
How personalized should the ad be? (1 – 3) 3
What zipcode do you live in? 80309
How old are you? 18
Cheap Product Ad: [Your text here]
How personalized should the ad be? (1 – 3) 3
What zipcode do you live in? 98195
How old are you? 21
Tourist Ad: [Your text here]