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

Pedro Rangel
Pedro Rangel
572 Points

Why is my code wrong I'm doing what the prompt is asking?

In raise an exception in python there is an objective that prompts you with "raise a ValueError if the product_idea is less than 3 characters long? product_idea is a string. " So its asking to raise an error when the amount of characters of the given product idea is less than 3 characters long this is my code that I entered:

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

try:
    x = input("Enter Product Name Here.  ")
    if len(x) < 3:
        raise ValueError("You need More than 3 characters to create a product!.")
    y = suggest(x)

except ValueError as er:
    print("Oh no! That's not a valid name. Please Try a name with 3 or more characters.")
else:
    print("Your products name is " "{}".format(y))

I ran the code in Visual Studio to first test it out like I do with most of my code before entering it into the objective. The code worked I was given an error for not having a product idea < 3 characters long and it prints the product idea with the suffix inator if the product is 3 or more characters

Id also like to mention I tried this way too maybe thinking the value error was suppose to be written inside the defined function

def suggest(product_idea):
    if len(product_idea) < 3:
        raise ValueError("You need More than 3 characters to create a product!.")
    return product_idea + "inator"

try:
    x = input("Enter Product Name Here.  ")
    y = suggest(x)
except ValueError as er:
    print("Oh no! That's not a valid name. Please Try a name with 3 or more characters.")
else:
    print("Your products name is " "{}".format(y))

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey Pedro Rangel 👋

You'll want to solely write the code that the challenge is asking for. In this instance the challenge only requires you to check if the value provided for the product_idea parameter is less than 3 characters and if so, raise a ValueError. You can add this logic to the function as you showed in your second code snippet, this way the logic will run whenever the function is being called rather than a single time like with your first snippet.

The only thing you'll want to adjust from your second code snippet is removing the try block, the tests for this challenge are not expecting anything else than the suggest function and will call that function with different values to see what is being returned. Therefor there's no need for prompting the user for a value 🙂

def suggest(product_idea): 
    if len(product_idea) < 3: 
        raise ValueError("You need More than 3 characters to create a product!.") 
    return product_idea + "inator"

Hope this clears things up! 😃

Pedro Rangel
Pedro Rangel
572 Points

Thank you I figured it out last night by just removing everything but the function I was being asked for and it worked. Thank you for answering though I love you I guess I was doing to much