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 trialNivesh Sharma
Courses Plus Student 1,941 PointsRaising an exception challenge errors
I am getting syntax error while raising the exception on a if condition using the len() function on the input string... Please explain the exception raising in detail...or suggest me a good resource
def suggest(product_idea):
return product_idea + "inator"
try:
product=input("")
if len(product)<3
raise ValueError
except ValueError:
print("Entry must be atleast 3 characters long")
else:
print(suggest(product))
2 Answers
Marcus Grant
Courses Plus Student 2,546 PointsHi Nivesh,
You have over complicated this challenge (I done the same).
Exception raising is actually done in the body of the function and only requires 2 lines of code.
I went down the rabbit hole of over complications on this challenge myself until I read an external resource.
Check this out: https://www.tutorialspoint.com/python/python_exceptions.htm
P.s.. I don't want to give you the exact answer as I believe the clue is enough to allow you to progress.
Alexander Davison
65,469 PointsYou should write your code in the suggest
function itself. Don't need to use try
or except
. Just write an if
statement in the suggest
function checking whether or not the len
of the product_idea
is less than 3, and if it is, raise a ValueError
.
Something like this:
def suggest(product_idea):
if <condition>:
#throw exception
return product_idea + "inator"
Replace <condition>
with the proper condition and #throw exception
with the proper raise
statement and it should pass!
Nivesh Sharma
Courses Plus Student 1,941 PointsNivesh Sharma
Courses Plus Student 1,941 PointsThank you Marcus...it really helped ...