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 value error

I'm trying to raise a value error but my code isn't working, don't know exactly what i'm doing wrong.

suggestinator.py
def suggest(product_idea):
    return product_idea + "inator"
    if suggest <= len(3):
        raise valueError()

3 Answers

Steven Parker
Steven Parker
229,657 Points

I see a few issues:

  • the testing (and possible raising) must be done before the return
  • name 'suggest' is not defined
  • an object of type 'int' (such as 3) has no len()
  • you'll want to test if the length of the argument is less than 3
  • "ValueError" is not a function (and should be spelled with a capital "V")
Gabrielle Lamarche
Gabrielle Lamarche
8,587 Points

Hey Matthew,

It looks like you're returning product_idea + "inator" before your code has a chance to run your if statement.

  1. Try moving your return statement to after the if statement
  2. You should be referencing the length product_idea since you can't reference a function you're declaring and product idea is a string based on the concatenation in your return statement.
  3. ValueError requires a capital V and E
  4. You might also want to include an error message when you're raising an error to make it more user-friendly; alternatively you can use the shorthand raise ValueError (no brackets)

I hope this is helpful!

def suggest(product_idea):
    if len(product_idea) <= len(3):
        raise ValueError('Sorry, that's too short!')
    return product_idea + "inator"
Sander Stantsits
Sander Stantsits
1,773 Points

The correct answer should be.

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

Hope this helps.