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

NameError: name 'product_idea' is not defined

Product_idea is not defined, any help? This is the assignment: Can you please raise a ValueError if the product_idea is less than 3 characters long? Kind regards

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

ERROR: test_exception_not_raised (main.TestRaiseExecution)

Traceback (most recent call last): File "", line 23, in test_exception_not_raised File "/workdir/utils/challenge.py", line 20, in execute_source exec(src) File "", line 3, in NameError: name 'product_idea' is not defined

======================================================================

ERROR: test_exception_raised (main.TestRaiseExecution)

Traceback (most recent call last): File "", line 30, in test_exception_raised File "/workdir/utils/challenge.py", line 20, in execute_source exec(src) File "", line 3, in NameError: name 'product_idea' is not defined

1 Answer

Three things:

  • Python is picky about indentation (the number of spaces before a line of code). Python thinks that the if portion of the code isn't within the function suggest
  • Once you properly indent the if statement, you must remember to put it before the return statement. return immediately breaks out of the function, so the if statement wouldn't ever be run unless you put it before the return
  • ValueError on its own refers to the error class itself, you need to make an instance of it using parentheses and you need to specify the error message in-between the parentheses

After fixing all of these errors, your code should look like:

def suggest(product_idea):
    if len(product_idea) <3:
        raise ValueError("Product idea too short")
    return product_idea + "inator"