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

Raising an exception challenge errors

I am getting syntax error while raising the exception on a if condition using the len() function on the input string... Please explain the exception raising in detail...or suggest me a good resource

suggestinator.py
def suggest(product_idea):
    return product_idea + "inator"
try:
    product=input("")
    if len(product)<3
        raise ValueError
except ValueError:
    print("Entry must be atleast 3 characters long")
else:
    print(suggest(product))

2 Answers

Marcus Grant
PLUS
Marcus Grant
Courses Plus Student 2,546 Points

Hi Nivesh,

You have over complicated this challenge (I done the same).

Exception raising is actually done in the body of the function and only requires 2 lines of code.

I went down the rabbit hole of over complications on this challenge myself until I read an external resource.

Check this out: https://www.tutorialspoint.com/python/python_exceptions.htm

P.s.. I don't want to give you the exact answer as I believe the clue is enough to allow you to progress.

Thank you Marcus...it really helped ...

You should write your code in the suggest function itself. Don't need to use try or except. Just write an if statement in the suggest function checking whether or not the len of the product_idea is less than 3, and if it is, raise a ValueError.

Something like this:

def suggest(product_idea):
    if <condition>:
        #throw exception
    return product_idea + "inator"

Replace <condition> with the proper condition and #throw exception with the proper raise statement and it should pass! :+1: