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 trialyahyaalshwaily2
Python Development Techdegree Graduate 20,049 PointsException Handling challenge problem
Hi, There's this challenge in Python basics course:
(I've made a function that creates brand new product names using "artificial intelligence".
I have a problem though, people keep on adding product ideas that are too short. It makes the suggestions look bad.
Can you please raise a ValueError if the product_idea is less than 3 characters long? product_idea is a string. Thanks in advance!)
I have made this
def suggest(product_idea):
if len(product_idea) <=3:
raise ValueError ("please give more than 3 characters")
return product_idea + "inator"
try:
product_idea = input("give the product a name ")
product_name = suggest(product_idea)
except ValueError as err:
print ("({})".format(err))
else:
print(product_name)
but I failed the tests and challenge. then I tried a much simpler script and it worked, it's below.
def suggest(product_idea):
if len(product_idea) <3:
raise ValueError ("too short")
return product_idea + "inator"
Could someone please explain why the simpler version passed but my initial one didn't?
Thank you!
1 Answer
Ave Nurme
20,907 PointsHi Yahya
This block is unnecessary as the challenge does not ask you to do that:
try:
product_idea = input("give the product a name ")
product_name = suggest(product_idea)
except ValueError as err:
print ("({})".format(err))
else:
print(product_name)
In your first block here...
def suggest(product_idea):
if len(product_idea) <=3:
raise ValueError ("please give more than 3 characters")
return product_idea + "inator"
... you need to change <=3
to <3
as the challenge text says to raise
a ValueError
if the product_idea
is less than 3 characters long. <= 3
means it's less than or equal to 3.
To raise an exception means using the raise
keyword and this is what the challenge task expects you to do.
I hope this helped to clarify things a bit!