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 Python Basics Functions and Looping Raise an Exception

Hi guys. What's wrong with my code?

suggestinator.py
def suggest(product_idea):
    if len (product_idea) <= 3:
        raise ValueError("Less than 3 characters long.")
        return product_idea + "inator"

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi Elsone Lottering

You are so very close. :)

It is just one small error. You have the return statement inside of the if block that is raising the ValueError because the string is too long, so the return cannot run. Just remove the indent so that it is outside of the if block but still inside the function. Then it will pass.

Just a couple of other notes to help you along. Coding is very specific and instructions should be carefully watched. Even though the your code will pass the challenge with that one fix, it wasn't exactly what was asked. You are asked to raise the ValueError if the product_idea is less than 3 characters, and your code is checking for less than and equal to 3 character, which can be a huge issue in real-world applications... so just watch for those tiny details.

Also, whitespace is really important in Python, so it's good practice to always abide by PEP8 guidelines on formatting. In the code, your if statement has an extra space between the method call and the parentheses =>

if len (product_idea) <= 3:
# should not have a space...
if len(product_idea) <= 3:

Otherwise, GREAT JOB!! 😀

:dizzy:

Hi Jason Anders

Thank you for the great response and for the extra tips. I will surely keep it in mind.

One more thing though, it is off topic, but I need help understanding the loop keyword. I need a different explanation to the one in the video, as I've watched the video a couple times, still can't seem to understand it.