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

Taylor Han
Taylor Han
3,701 Points

Raising a ValueError Statement

Hello,

I am trying to trigger the raise ValueError function, but I think my if statement is incorrect.

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

if len(suggest) < 3
    raise ValueError ("At least three characters, please")
Taylor Han
Taylor Han
3,701 Points

I would like the if statement to occur if the suggested product name is less than three characters, but I tried a few different ways and don't really understand how to do this.

2 Answers

Hey! Great start! I have a few ideas for you.

You'll want to test product_idea because it's actually the text that the user would be inputing (not suggestion).

If you look at the check_please.py file that you created for the activity, you can see the proper format - all the if statements are before the return statement. You'll also need a colon after the 3 (it took me forever to figure that out that tiny error - blurgh!), and for this activity the characters would be have to be less than or equal to 3.

if len(product_idea) <= 3: raise.... return... I hope this helps!

Taylor Han
Taylor Han
3,701 Points

Thank you! I made those changes, but now the challenge is saying that it is passing in a single letter 'a' expecting the value error to be raised but it is not. Any ideas?

Hmmm I'm not sure without seeing it :( Is len(product_idea) <= 3: placed right after the define in line one and is it indented properly? Maybe it's the syntax of the if or the raise statement since you've now for sure got them in the correct order before return?

Your if statement should come before your return statement. And you should be testing the length of product_idea.