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: Raising Value Error

Question: 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!

I'm not sure how to check how many characters are in a string... Help?

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

3 Answers

Hi matthew,

You can check the length of a string by passing the string in question as an argument to the len() function.

my_name = β€˜Brandon’
print(len(my_name))
# output: 7

Thanks, soo much!

I'm still not passing the quiz... what am I doing wrong?

Post your code here and I’ll evaluate it for you. Tell you where the bug may lie.

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!

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

OK matthew,

I see the problem. You wrote the logic for your code in the wrong order is all.

Before I get to that I want to inform you that anything after the python interpreter encounters your return statement is unreachable code.

As soon as it evaluated the return statement, the python interpreter exits the function.

So imagine you have a function like this:

def some_func():
    return 2
    name = β€˜Brandon’
    return name
# the name variable is never created, and 2 is returned from the function

So with the challenge, you need to make sure that you raise the value error before you try to return anything.

Also the product idea has to be less than 3 characters long in order for the value error to be raised. So you don’t want to use the less than or equal to comparison operator in this situation. Unless your test is the following:

if len(product_idea) <= 2: ...

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

Hope that helps.

Wow, thank you so much for the time you invested in helping me. I really appreciate your help. Got it!! Thanks.