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

Why does my code keep showing errors??

I cannot get this code below to actually read the character count of the input (product_idea) and then show the error message to the user. Then, an additional error occurs when the code displays the final message of "Thank you for suggesting the -----.") None of it seems to work properly, and I can't figure out why....Can anyone offer some wisdom?

def suggest(product_idea): if len(product_idea) < 3: raise ValueError("Please suggest a product name using three or more characters.")
return product_idea + "inator"

try: product_idea = input("What is your product suggestion? ") except ValueError as err: print("Whoops! That's an invalid response...") print("{}").format(err) else: print("Thank you for suggesting the {}".format(product_idea(suggest)))

suggestinator.py
def suggest(product_idea):
    if len(product_idea) < 3:
        raise ValueError("Please suggest a product name using three or more characters.")    
    return product_idea + "inator"                        

try:
    product_idea = input("What is your product suggestion?   ") 
except ValueError as err:
    print("Whoops! That's an invalid response...")
    print("{}").format(err)
else:
    print("Thank you for suggesting the {}".format(product_idea(suggest)))

2 Answers

Oh, that helps so much! I was missing a very crucial line. Thank you!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Rachel,

your format() method arguments are switched. Try this:

print("Thank you for suggesting the {}".format(suggest(product_idea)))

The suggest method expects the input argument, not the input argument takes the suggest method :smile:

I hope this makes sense

Thank you! Yes, I figured that might be one of the issues with this code. I updated that, but there is still an additional error that I can't get to the bottom of. I ran the updated code, and input a 2-character response when prompted. Here is what I got back:

treehouse:~/workspace$ python raise.py
What is your product suggestion? ed
Traceback (most recent call last):
File "raise.py", line 12, in <module>
print("Thank you for suggesting the {}".format(suggest(product_idea)))
File "raise.py", line 3, in suggest
raise ValueError("Please suggest a product name using three or more characters.")
ValueError: Please suggest a product name using three or more characters.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Rachel,

the code below should work. Basically, the try section will call the suggest method and store the result (either ValueError or the correct input) in the variable newName. If newName contains a ValueError, it will be "excepted" and displayed to the user.

def suggest(product_idea):
    while len(product_idea) < 3:
        raise ValueError("Please type in something 3 characters or more.")
    return product_idea + "inator"

try:
    suggestion = input("What product idea were you thinking of?   ")
    newName = suggest(suggestion)
    print("This should be your product idea name, {}".format(newName))
except ValueError as err:
    print("{}".format(err))

Makes sense?

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

No problem, I am happy to help!