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

suggestinator.py

im kinda stuck, is there something im doing wrong?

suggestinator.py
def suggest(product_idea):
    return product_idea + "inator"
if len(product_idea) <= 3:
    raise ValueError
    print("Needs more than 3 character")

my error was a NameError: and says that the "product_idea is not define?

ooh, Nevermind i got it!!!

1 Answer

Josh Keenan
Josh Keenan
20,315 Points

You have the right idea here, and the code is logically sound but you have put it in the wrong order. In Python return statements are the end of the function, so your function at present runs these lines every time, but skips the rest.

def suggest(product_idea):
    return product_idea + "inator"

So your code has to slot in before the return statement. The other issue is the indentation, as you need to make sure your code is inside the function and in Python we use indentation instead of curly braces to do so.

def suggest(product_idea):
    if len(product_idea) < 3:
        raise ValueError
    else:
        return product_idea + "inator"

Now the print statement you have won't run, as raising an error is also a function exit point. The else here isn't needed but I have included it for clarity. If you have any questions don't hesitate to ask. Hope this helps!

so i got it right, but i did not put an else statement, will the code run if i didn't use a else statement? it still says that i got it right. Thank you very much

Josh Keenan
Josh Keenan
20,315 Points

As I said it isn't needed but just for clarity, in this instance if the length was less than 3, a value error is raised and the function terminates. This means that even without the else, if this condition is met the code after it won't be run.