// Data Structures
Dict Mini Project — Pet Shop
What you're building
A pet shop manager. Each animal has a name, species, price, and availability.
The user can view all animals, search by name, and mark one as sold.
What you need to know first
- Nested dicts
items()to loop over key-value pairsinandget()for safe lookupwhile Truewithbreakinput()
The brief
Build a program that:
- Starts with a pre-filled nested dict — at least four animals
- Shows a menu:
1 — View all,2 — Search,3 — Buy,4 — Quit - On view: lists every animal with species, price, and availability
- On search: asks for a name, prints that animal's details or "not found"
- On buy: asks for a name, marks it as sold if available — or reports it's already gone
- On quit: exits cleanly
Think before you code
- What keys does each animal's inner dict need?
- How do you check if an animal exists before searching or buying?
- How do you mark an animal as sold without removing it from the dict?
- What do you show when an animal is already sold?
Your starting point
shop = {
"Rex": {"species": "Dog", "price": 350.00, "available": True},
"Whisker": {"species": "Cat", "price": 180.00, "available": True},
"Tango": {"species": "Parrot", "price": 220.00, "available": True},
"Bubbles": {"species": "Fish", "price": 25.00, "available": True}
}
while True:
print("\n1 — View all")
print("2 — Search")
print("3 — Buy")
print("4 — Quit")
choice = input("Your choice: ")
# your code here
Expected output
Your choice: 1
Available animals:
- Rex Dog $350.00 available
- Whisker Cat $180.00 available
- Tango Parrot $220.00 available
- Bubbles Fish $ 25.00 available
Your choice: 2
Search: Tango
Tango — Parrot — $220.00 — available
Your choice: 3
Buy: Rex
Rex sold. Good choice.
Your choice: 1
Available animals:
- Rex Dog $350.00 SOLD
- Whisker Cat $180.00 available
- Tango Parrot $220.00 available
- Bubbles Fish $ 25.00 available
Your choice: 3
Buy: Rex
Rex is already sold.
Your choice: 4
Out.
Heads up!
- Use
.capitalize()on input so "rex" and "REX" both match "Rex" - Marking as sold means setting
"available"toFalse— not removing the entry - Check existence with
inbefore accessing any inner key - View all shows every animal — sold and available
The solution
shop = {
"Rex": {"species": "Dog", "price": 350.00, "available": True},
"Whisker": {"species": "Cat", "price": 180.00, "available": True},
"Tango": {"species": "Parrot", "price": 220.00, "available": True},
"Bubbles": {"species": "Fish", "price": 25.00, "available": True}
}
while True:
print("\n1 — View all")
print("2 — Search")
print("3 — Buy")
print("4 — Quit")
choice = input("Your choice: ")
if choice == "1":
print()
for name, data in shop.items():
status = "available" if data["available"] else "SOLD"
print(f"- {name:<10} {data['species']:<8} ${data['price']:>6.2f} {status}")
elif choice == "2":
name = input("Search: ").capitalize()
if name in shop:
data = shop[name]
status = "available" if data["available"] else "SOLD"
print(f"\n{name} — {data['species']} — ${data['price']:.2f} — {status}")
else:
print(f'"{name}" not found.')
elif choice == "3":
name = input("Buy: ").capitalize()
if name in shop:
if shop[name]["available"]:
shop[name]["available"] = False
print(f"{name} sold. Good choice.")
else:
print(f"{name} is already sold.")
else:
print(f'"{name}" not found.')
elif choice == "4":
print("Out.")
break
else:
print("Invalid choice. Enter 1 to 4.")
// resources
// 0 comments
// No comments yet. Be the first.
// leave a comment