Pet Adoption Kiosk
Blue Ridge Paws, a small animal shelter, wants a kiosk in its lobby where visitors can see the pets waiting for a home, find a good match, and adopt one.
The shelter keeps everything in one big dictionary, holding a list, holding a dictionary for each pet, holding a list of that pet's traits.
In this lab you'll write the code that finds its way through that data and changes it.
Along the way you'll use loops, lists, dictionaries, f-strings, int(), and a module you import.
You can start before class
Steps 1–2 take about fifteen minutes, and you can do them on your own. If you get stuck, that's what class is for; we'll help you get unstuck!
Map of the lab
| Step | File | What you'll write | About |
|---|---|---|---|
| 1 | shelter_data.py |
Predictions | 10 min |
| 2 | shelter.py |
available() |
5 min |
| 3 | shelter.py |
find_matches() |
10 min |
| 4 | shelter.py |
adopt() |
10 min |
| 5 | kiosk.py |
Menu option 1 | 10 min |
| 6 | kiosk.py |
Menu option 2 | 10 min |
shelter_data.py holds the data, and you won't change it.
shelter.py holds the logic: functions that take data in and return results, with no input() or print().
kiosk.py is the menu that talks to the visitor and calls those functions.
Your project will be organized the same way.
Getting Started
Create a Week12 folder in your CS149 folder.
Download the following files into it.
(Right click each link, and "Save link as…")
- shelter.py and kiosk.py, the files you'll finish
- shelter_data.py, the data
- test_shelter.py and test_kiosk.py, the tests
Open shelter_data.py in VS Code and look it over.
SHELTER is one big dictionary, and its "pets" key holds a list.
A pet whose "adopter" is None is still waiting for a home.
Step 1: Explore the Data
Open shelter_data.py in VS Code.
For each expression, find the value by reading the file, and write down your prediction.
If you think it will crash, name the error.
| Expression | Your prediction |
|---|---|
SHELTER["name"] |
|
len(SHELTER["pets"]) |
|
SHELTER["pets"][0]["name"] |
|
SHELTER["pets"][3]["adopter"] |
|
SHELTER["pets"][5]["traits"][2] |
|
SHELTER["pets"]["Biscuit"] |
Check your answers
'Blue Ridge Paws', 8, 'Biscuit', 'Jordan', and 'good with kids'.
The last one is a TypeError: SHELTER["pets"] is a list, and a list takes a position, not a name.
Keep this picture in your head
Each function you write takes the shelter as a parameter named shelter, and the kiosk passes in SHELTER, so inside your functions they're the same dictionary.
shelter is a dict. shelter["pets"] is a list. Each item in that list is one pet's dict. pet["traits"] is a list of strings.
Before you write each function, ask: which of those am I working with?
Step 2: available
available(shelter) returns a new list of the pet dictionaries whose "adopter" is None, in the same order as in the shelter.
Get a hint
Fill in the middle:
waiting = []
for pet in shelter["pets"]:
# if this pet's adopter is None, append the pet to waiting
return waiting
To check for None, write pet["adopter"] is None.
Then run pytest test_shelter.py.
test_available should pass.
Step 3: find_matches
A visitor tells the kiosk three things: the species they want, the oldest age they'd consider, and one trait that matters to them.
find_matches() returns a list of the names of the available pets that fit all three.
It looks a lot like Step 2, with two changes:
- Loop over
available(shelter)instead ofshelter["pets"], so adopted pets are already left out. - Append
pet["name"], not the wholepet.
The if needs three checks joined with and: the species matches, the age is at most max_age, and the pet has the trait.
Since pet["traits"] is a list, trait in pet["traits"] checks it in one step.
What it should do
find_matches(SHELTER, "dog", 5, "good with kids") returns ["Biscuit", "Juniper"]: both are available dogs, 5 or younger, with that trait.
Step 4: adopt
adopt(shelter, name, adopter) finds the available pet with that name and sets its "adopter" to the person's name.
It returns True if it adopted the pet.
It returns False, and changes nothing, if no pet has that name or that pet already has a home.
Get a hint
Why does this work?
available() gives back the same pet dictionaries that are inside shelter, not copies.
So when you change pet["adopter"], the shelter changes too.
And because you loop over available(shelter), trying to adopt a pet who already has a home returns False with no extra code.
Step 5: See the Pets (menu option 1)
Open kiosk.py and read it before you change anything.
The menu loop and option 3 are already written.
Notice the first two lines.
import shelter as sh makes your own file a module, so sh.adopt(...) calls the function you wrote in Step 4.
from shelter_data import SHELTER brings in the data, and the kiosk passes SHELTER to each of your functions.
Every test in test_shelter.py should pass before you start here.
Replace the pass under option 1 with a loop over sh.available(SHELTER) that prints one line per pet, in columns:
Biscuit dog age 3 playful, good with kids
Mochi cat age 1 playful, curious
Captain dog age 9 calm, good with cats
- The name is left-aligned in 10 characters and the species in 8, like
{name:<10}in the f-strings lab. - Then
age, a space, and the age left-aligned in 4. - Then the traits, joined into one string with
", ".join(pet["traits"]).
Quotes inside an f-string
The f-string uses double quotes, so write keys with single quotes inside the braces: f"{pet['name']:<10}".
Step 6: Find a Match (menu option 2)
Ask these three questions, in this order:
Then call sh.find_matches() and print the result:
- no matches:
No matches yet. Check back soon! - otherwise: the number of matches, then
match(es):, then the names joined with", ", such as2 match(es): Biscuit, Juniper
input() always gives you a string
find_matches() compares the age with <=, and "5" <= 9 is a TypeError.
Convert the age with int() first.
Run pytest test_kiosk.py. Everything should pass.
Take it for a spin
Run kiosk.py for real:
- See who's waiting with option 1.
- Find a match for yourself with option 2, then adopt that pet with option 3.
- Choose option 1 again. Your pet is gone from the list, because
adopt()changed its dictionary insideSHELTER. - Choose 0 to exit, then run the kiosk again. Everyone is back, since the data starts fresh from
shelter_data.pyeach run.
Extra challenges
- Forgiving input. Visitors type
Doganddog. Use.strip().lower()so they still match. Then, if they typefivefor the age, the kiosk crashes; use awhileloop andisdigit()to keep asking until they type a number. - Trait report. Write
count_traits(shelter), which returns a dictionary from each trait to how many available pets have it, such as{"playful": 3, "good with kids": 3, ...}. You'll need a loop inside a loop. Add it to the menu as option 4, printing onetrait: countline per trait. (Remember to add the option toMENUtoo.) - New arrivals. Download arrivals.csv into your
Week12folder. Writeread_arrivals(path), which reads the file and returns a list with one pet dictionary per line, skipping the header. Each dictionary has the same keys as the pets inshelter_data.py: make the age anint, split the traits on;into a list, and set the adopter toNone. Watch out for Smokey, who has no traits! Inkiosk.py, before the menu loop, add the new pets toSHELTER["pets"], then check that option 1 lists them.
Submission
Upload shelter.py and kiosk.py to Gradescope.
The autograder runs the provided test_shelter.py and test_kiosk.py.