Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,191 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.