Python Python Basics Functions and Looping Raise an Exception

Christopher Hill
Christopher Hill
1,042 Points

I'm needing help raising a ValueError

I've made a function that creates brand new product names using "artificial intelligence".

I have a problem though, pe

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

4 Answers

Ave Nurme
Ave Nurme
15,861 Points

Hi Christopher

You are very close! You are just missing the len() here and all should be good:

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

As the text in the code challenge says -> people keep on adding product ideas that are too short. So you need to check the length of product_idea and you can do it like this:

if len(product_idea) <= 3:

EDIT: You also might want to change the message here in quotes:

raise ValueError("product_value")

Maybe something like "The length of you product idea is too short".

Hope this helps!

You have a total of Three errors, a TypeError and a Syntax error and a challenge requirement error in your code.

def suggest(product_idea):
    if product_idea <= 3:   #TypeError and challenge requirement error
        raise ValueError("produc_value" <= 3) #Invalid syntax
    return product_idea + "inator"

The TypeError is because you are comparing a string product_idea and an int <=3 which don't always get along. I want you to find a way to determine the length of the product_idea and compare that to <=3 which should work.

The Challenge requires you to insert a condition for less than three words NOT less than.

and finally raise ValueError("produc_value" <=3) is an invalid syntax or simply this is wrong grammar, this sentence to the program makes no sense. I want you to experiment with the ("produc_value" <=3) part and find what is grammatically wrong or maybe a type error.

If you tried but failed, and need the solution just comment on this post.

Christopher Hill
Christopher Hill
1,042 Points

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

After fixing the grammatical error, I'm totally clueless about the next step to run the code.

Ave Nurme
Ave Nurme
15,861 Points

No problem, Christopher, happy to help!