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

Kaitlyn Satterlund
Kaitlyn Satterlund
436 Points

Product naming: too short input exception

I am unsure how to raise this exception, my code attempt is below. Please help!

suggestinator.py
def suggest(product_idea):
    return product_idea + "inator"
if len product_idea <= 3 
raise ValueError as err:
        print(product name too short)

2 Answers

Your function ends with the line return product_idea + "inator" and ignores everything typed in the function after it returns. In this case, your return line is the only line in the body of the function, since the next line is not indented.

if len product_idea <= 3 should be if len(product_idea) <= 3: since len should have parentheses around its argument and if statements use :.

print(product name too short) would try to print out the value of the variables product, name, to, and short. If you surround your message with quotes, as in print("product name too short"), the words will be treated as words and not as variables.

An example of raising an exception would be

if True:
    raise ValueError("Error message here")

Your current attempt at raising an exception resembles the code for catching an exception.

try:
    raise ValueError("Error message here")
except ValueError as err:
    print(err)
Szymon Dabrowski
Szymon Dabrowski
2,207 Points

I am guessing that was from the raise an objective challenge

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

This is the solution.