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

I don't understand why the code isn't working?

I have the raised value error, but it's saying I have not raised a suggestion

suggestinator.py
import math

def suggest(product_idea):
    if product_idea "3":
        raise ValueError("The suggestion needs to be greater than 3 characters!")
    else:
        return product_idea + "inator"

5 Answers

Scott Bailey
Scott Bailey
13,190 Points

The raise ValueError is ok, the issue is with your "if" statement.

You need to compare then len() of the product_idea and see if it is less than 3

The import of math wouldn't be needed as well - pythons built in will be all you need!

If you need any more help please say!

Can you explain why I need to type len(product_idea) <3: instead of without?

I'm trying to understand the why >_>

Scott Bailey
Scott Bailey
13,190 Points

Because product idea will be a string (they state in the challenge) - len(product_idea) will return how many characters are in the string, without doing this it is just a string and length isn’t considered in the argument. With python you need to be explicit.

Now you have the length of product idea you need to use python to see if that’s larger than 3 - this will return true or false and either cause the error to be raised or not.

The main thing is len() will give you a number. That number can then be used in the comparison < 3

ok, I think I understand. Going back to my code

My code doesn't work:

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

Scott Bailey
Scott Bailey
13,190 Points
def suggest(product_idea): 
    if len(product_idea) < 3: 
        raise ValueError("Error") 
    else: 
        return product_idea + "inator"

I think there may have been an indentation error on one of the statements - I put your code through and it passed. I put the passing code above (formatted)

Try refreshing the challenge page as well, I sometimes have an issue where my code will fail but after a refresh and paste back in it works fine.