Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Using Databases in Python Gettin' CRUD-y With It CRUD: Search Function

Devin Scheu
Devin Scheu
66,191 Points

Pewee Help Phython

Question: Create a function named search_challenges that takes two arguments, name and language. Return all Challenges where the name field contains name argument and the language field is equal to the language argument. Use == for equality. You don't need boolean and or binary & for this, just put both conditions in your where().

Can I get a step by step walk through and break down of this challenge because i'm confused.

Code:

crud.py
from models import Challenge


def create_challenge(name, language, steps=1):
    Challenge.create(name=name,
                     language=language,
                     steps=steps)

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Devin,

I hope I can make it clear:

first create the function:

def search_challenges(name, language):

Pretty standard, I am sure this is not a problem for you.

Inside the function, we need to SELECT (and return) all the challenges that match those 2 criteria without using and or &; so we simply separate the 2 conditions with a comma:

How do we write the conditions?

First one, challenge name contains name:

Challenge.name.contains(name)

Second one, Challenge.language== language.

So, to conclude we can actually write a single command inside the function like this:

return Challenge.select().where(Challenge.name.contains(name), Challenge.language==language)

Select is the database command to retrieve information and where is used to match the conditions. Since code challenge does not want us to use AND or &, we can use a comma to separate the 2 needed conditions.

I had to paste the final code even if I do not like it but it was needed to understand it better I think.

Let me know if ok

Vittorio

Rafael Pastrana
Rafael Pastrana
3,020 Points

Super answer! Thanks, Vittorio!

Andrew Winkler
Andrew Winkler
37,739 Points

This was an amazing breakdown. Thank you.