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

Trouble with Code Challenge: Raise exceptions

So I am in the middle of a challenge and I can not figure out what I am doing wrong. I figured I needed an input for the program to read how many characters there are in the string to determine if an error needs to be raised, but my code is flawed somewhere, I know this has to be easy, and I am over thinking it. Please help.

Here is the challenge: Can you please raise a ValueError if the product_idea is less than 3 characters long?

This is my code: product_idea is a string. def suggest(product_idea): product_idea = input("What is a name for product? ") if product_idea < str (3): raise ValueError ("Characters need to be 3 or more!")

else:
    return product_idea + "inator"

This is errors I am getting:

Ran 1 test in 0.000s

OK

EE

ERROR: test_exception_not_raised (main.TestRaiseExecution)

Traceback (most recent call last): File "", line 24, in test_exception_not_raised File "", line 2, in suggest EOFError: EOF when reading a line

======================================================================

ERROR: test_exception_raised (main.TestRaiseExecution)

Traceback (most recent call last): File "", line 32, in test_exception_raised File "", line 2, in suggest EOFError: EOF when reading a line

2 Answers

mouseandweb
mouseandweb
13,758 Points

Hello Jacob McKinney ,

We cannot compare string and integer, so I assume that is why you typed this:

product_idea < str (3):

But this would compare the 12 characters in the string, product-idea, with the one character in the string, 3. What you want to do is compare the amount of characters in string, product_idea, to the integer 3. Like this:

product_idea < 3:

This won't work either! Because we are still missing the last part, which is that we are still comparing the string, product_idea, with the integer, 3. We cannot compare a string with an integer. We need to use the len() function!

len(product_idea) < 3:

The len() function returns an integer representing the length of the string passed to it. The input comes in as a string so product_idea is a string. Now we are comparing the length (an integer) of the string, product_idea, with the integer, 3.

You can continue that:

def suggest(product_idea):
    product_idea = input("What is a name for product? ")
    if len(product_idea) < 3:
        raise ValueError("Characters need to be 3 or more!")

I hope this helps. Reach out if you have any other questions or need a follow-up.

it looks like you are checking that product_idea is less than the string "3", rather than checking if product_idea has fewer than 3 characters