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
Ayman Said
Python Web Development Techdegree Student 14,717 PointsWhat's wrong with my code!!!! - suggestinator challenge @Unit1, Plz help
The challenge: Can you please raise a ValueError if the product_idea is less than 3 characters long? product_idea is a string. Click here for the challenge page
My code:
def suggest(product_idea):
try:
if len(product_idea) < 3:
raise ValueError("product idea name is too short!")
except ValueError as err:
print("({})".format(err))
else:
return product_idea + "inator"
I think my code is correct, yet the Check Work button reports that it's NOT!! stating: I passed in 'a' and expected a ValueError to be raised
I run my code using Repl.it and it works like a charm! (also while passing 'a'). What's wrong?
Thanks in advance, Ayman.
1 Answer
Louise St. Germain
19,425 PointsHi Ayman,
There's nothing wrong with your code per se, but the challenge is expecting that your function will directly output an exception if the input string is less than 3 characters. Normally you wouldn't want to do this but for this challenge, that's what they want. In your case, you raise the exception but then you handle it internally, so the challenge testing program never sees the actual exception.
You can fix it by taking out your exception handling and just raise the ValueError, so that your function output behaves as follows, when seen from the outside:
suggest("Happy") --> "Happyinator"
suggest("a") --> big ugly ValueError!
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHI, Louise St. Germain ! I took the liberty of changing your comment to an "Answer". Thanks for helping out in the Community!
Ayman Said
Python Web Development Techdegree Student 14,717 PointsAyman Said
Python Web Development Techdegree Student 14,717 PointsThanks, Louise for the clarification.
Louise St. Germain
19,425 PointsLouise St. Germain
19,425 PointsThanks Jennifer! I'm pretty new here and I did discover that there is a difference between answers and comments, but I didn't figure that out until some time after this post! :-)