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

Samuel Adjei
seal-mask
.a{fill-rule:evenodd;}techdegree
Samuel Adjei
Full Stack JavaScript Techdegree Student 4,789 Points

Issue with exception handling within a function - Python

Hi All,

I have written this code and when I submit, it produces an error, but everything is fine in my terminal? Please help?!

def suggest(product_idea):
    return product_idea + "inator"

try:
    product_idea = input("What is your product idea? ")
    if len(product_idea) <= 3: #to catch the exception of people trying to use LESS THAN 3 characters
        raise ValueError("Your product Idea is too short. Please re-submit with more than 3 characters.") #raise is used here to capture the error
except ValueError as err: #this is to handle the error from line 8
        print("Oh no! That's not a valid value. Try again...")
        print("({})".format(err)) #this is to handle the error from line 8
else:
        print("Great idea!")

Here is the error I get: Bummer: Oh no, there were 2 problems with your code, check the preview pane for more information

.

Ran 1 test in 0.000s

OK

EE

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 5, in EOFError: EOF when reading a line

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

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 5, in EOFError: EOF when reading a line


Ran 2 tests in 0.002s

FAILED (errors=2)

suggestinator.py
def suggest(product_idea):
    return product_idea + "inator"

try:
    product_idea = input("What is your product idea? ")
    if len(product_idea) <= 3: #to catch the exception of people trying to use this, that are less than 1
        raise ValueError("Your product Idea is too short. Please re-submit with more than 3 characters.") #raise is used here to capture the error
except ValueError as err: #this is to handle the error from line 6
        print("Oh no! That's not a valid value. Try again...")
        print("({})".format(err)) #this is to handle the error from line 6
else:
        print("Great idea!")

3 Answers

Steven Parker
Steven Parker
231,269 Points

You're working way to hard! The challenge only asks you to modify the function itself to test the value ("if") and "raise" an exception. So you only need to add 2 lines of code.

You won't need to get input, call the function, capture an error, or print anything.

Kristen Cordes
Kristen Cordes
807 Points

I think I'm close...but I think I have the wrong ??? in front of the (3).

def suggest(product_idea): if product_idea < str(3): raise ValueError("Your idea must be longer than 3 characters long") return product_idea + "inator"

Says I have and AssertionError:ValueError not raised : I passed in 'a' and expected a ValueError to be raised.

Any help.... Thanks, K

Steven Parker
Steven Parker
231,269 Points

Kristen, your should always start a fresh question when you have a problem that hasn't already been answered.

But I'll give you a hint that you probably want to test the length of the input instead of the input itself.

And when posting code, always use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down: Or watch this video on code formatting.