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

Simon Bazso
Simon Bazso
1,162 Points

Hello, im stuck trying to raise an exception.

Hello! They want me to raise an exception for when the user tries to add a word thats less than three letters. What am i doing wrong? Im really stuck, thanks!

suggestinator.py
def suggest(product_idea):
    if product_idea < 3:
        raise ValueError("Your name is to short, try again")
    return product_idea + "inator"
try:
    product_idea = int(input("Whats your idea?   "))
except ValueError:
    print("Ops, your name doesnt work, try again!  ")

1 Answer

Eric M
Eric M
11,546 Points

Hi Simon,

There's no need to use try/except blocks for this task or handle the user input. All we need to do is raise a ValueError if the product idea has less than 3 letters.

When you're checking this condition you're code as posted checks the value of product idea, we want to check the number of letters. product_idea is getting "inator" a String literal added on the end of it, so product_idea is probably a String. This means we can use the len() function to return the number of letters.

Your initial raising of the value error would have worked fine if the if statement was checking the length of product_idea against 3 instead of product_idea's value.

def suggest(product_idea):
        if len(product_idea) < 3:
                raise ValueError("Your name is too short, try again")
        return product_idea + "inator"
Simon Bazso
Simon Bazso
1,162 Points

Thanks Eric! Worked out after i had to fix some indent issues :*D