Python Python Basics Functions and Looping Raise an Exception

Blue DiamondXD
Blue DiamondXD
4,978 Points

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

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

1 Answer

Josh Keenan
Josh Keenan
17,380 Points

To complete this challenge you need to use len() to get the length of the string.

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

Here's my solution, it checks if the length of the string product_idea is less than 3, if it is then it will raise the value error, if it doesn't trigger this clause, then it will just go to the return statement. Hope this makes sense.

Blue DiamondXD
Blue DiamondXD
4,978 Points

omg i didnt for form the the first place then i thy different methos and then i capy and paste and it work HOW!

Josh Keenan
Josh Keenan
17,380 Points

Happy to help, your code has a few issues, firstly you fail to check the length of the string at any point which is what we need to be examining, you also used <= instead of < which would cause the code to fail as these are different.

One will accept the string that is 3 characters and the other won't.