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

Whats wrong with my code

I keep getting the following error TypeError: '<' not supported between instances of 'str' and 'int'

4 Answers

Kent Åsvang
Kent Åsvang
18,823 Points

You need to convert your string to an integer, the 'smaller than' ( '<' ) comparison operator don't work with strings.

when i try to convert the string to an integer with int() i get the following error ValueError: invalid literal for int() with base 10: 'bacon'

Kent Åsvang
Kent Åsvang
18,823 Points

If you tell me what the challenge is or if you copy/paste your code I could try and see if I can help you to see whats wrong

it's the raise an exception challenge in python basics we're trying to raise a ValueError exception if the product idea, which the user inputs is less the 3 characters long

def suggest(product_idea):
    if int(product_idea) < 3:
        raise ValueError
    return product_idea + "inator"
Kent Åsvang
Kent Åsvang
18,823 Points

Okey, python has a built-in function called len(), it takes a string as argument, and returns its length as an integer, which can be used with the comparison-operator.

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

thank you

len is the action that makes sure to count the words right? If that makes sense I am literally just starting out, and I am loving treehouse over codecademy but somethings I still need help on.