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

challenge task problem

Can you solve this for me?? i didn't get it I've made a function that creates brand new product names using "artificial intelligence".

I have a problem though, people keep on adding product ideas that are too short. It makes the suggestions look bad.

Can you please raise a ValueError if the product_idea is less than 3 characters long? product_idea is a string. Thanks in advance!

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

suggestinator.py
def suggest(product_idea):
    return product_idea + "inator"
user_idea = input("please enter your idea ")
try:
    if len(user_idea) <= 3:
        raise ValueError("the product idea is a string")
except ValueError as err:
    print("the {} is a string".format(err))

(See my answer below; I accidentally posted my answer as a comment...)

3 Answers

Hi Kishan,

Yes, this lesson was confusing for me too. You've made a very good attempt though. I see your above message actually blends together the teacher's instructions. So, just for clarity:

# Starting code:
def suggest(product_idea):
    """ I've made a function that creates brand new product names using "artificial intelligence".
        I have a problem though, people keep on adding product ideas that are too short. 
        It makes the suggestions look bad.
        Can you please raise a ValueError if the product_idea is less than 3 characters long? 
        product_idea is a string. 
    """
    return product_idea + "inator"

That's the original code and teacher's instructions. Again, you have a great start to it. However, you don't need the input() command. I would suggest using some of your code lines using this approach

def suggest(product_idea):
    if len(product_idea) < 3:
        raise ValueError("<<TYPE HERE THE REASON FOR THE ERROR>>")
    try:
        # PLEASE ADD ONE LINE HERE
    except ValueError as err:
        # PLEASE ADD ONE LINE HERE

I hope this works. Let me know. And good luck!

You don't need try/except; there's no error handling needed. You just need to throw an error and otherwise return the value normally.

Antonio De Rose
Antonio De Rose
20,885 Points

Mark Chesney give your answers in the answer area, your answer can be voted and high, and may also be the best answer.

thank you, sir!

Bro, i didn't this can you solve for me with proper code so it works. it's little bit confusing.

Alexander's response was helpful for me -- thank you Alexander!

Kishan, please replace CONDITION below with the inequality expression. I'll give you a hint below in comments.

def suggest(product_idea):
    if (CONDITION):
        raise ValueError
    return product_idea + "inator"

# hint: Two observations about your original expression: len(user_idea) <= 3
# 1) It should use the parameter that's passed in, from line 1, instead of user_idea
# 2) The instructions ask for "less than 3 characters long", so if it is exactly 3 characters long, the code should not raise an error.

Best of luck!