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 trialAndrei Oprescu
9,547 Pointsusing .where() in a challenge
Hello!
I have a challenge with a question like this:
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().
The code that I created for this challenge is at the bottom of this post.
Can someone tell me hoe to implement .where() in my code and how to fix it?
Thanks!
Andrei
from models import Challenge
def create_challenge(name, language, steps=1):
Challenge.create(name=name,
language=language,
steps=steps)
def search_challenges(name, language):
challenges = Challenge.select()
if name, language == challenges.where(name, language):
return challenge
2 Answers
Steven Parker
231,269 PointsHere's a few hints:
- you can chain
.where
on to the end of theselect()
- the arguments to
.where
should be complete comparison expressions - you won't need a "if" statement
Andrei Oprescu
9,547 PointsHi!
Thank you for your hint! That helped me a lot to understand the use of .where() and to understand how to do this challenge.
Thanks!
Andrei
Andrei Oprescu
9,547 PointsAndrei Oprescu
9,547 PointsHi again!
I took your hints to what my code must look like and I have come to this code:
Can you tell me why this code doesn't work?
Thanks!
Andrei
Steven Parker
231,269 PointsSteven Parker
231,269 PointsYou're really close now! But the challenge doesn't want only cases where the name is an exact match, the instructions said "...where the name field contains name argument".
That's a bit of a hint in itself, since strings have a "contains" method that could be really handy here.