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 trialMathew Yangang
4,433 PointsRaising value error
I'm trying to raise a value error but my code isn't working, don't know exactly what i'm doing wrong.
def suggest(product_idea):
return product_idea + "inator"
if suggest <= len(3):
raise valueError()
3 Answers
Steven Parker
231,275 PointsI see a few issues:
- the testing (and possible raising) must be done before the return
- name 'suggest' is not defined
- an object of type 'int' (such as 3) has no
len()
- you'll want to test if the length of the argument is less than 3
- "ValueError" is not a function (and should be spelled with a capital "V")
Gabrielle Lamarche
8,587 PointsHey Matthew,
It looks like you're returning product_idea + "inator" before your code has a chance to run your if statement.
- Try moving your return statement to after the if statement
- You should be referencing the length product_idea since you can't reference a function you're declaring and product idea is a string based on the concatenation in your return statement.
- ValueError requires a capital V and E
- You might also want to include an error message when you're raising an error to make it more user-friendly; alternatively you can use the shorthand
raise ValueError
(no brackets)
I hope this is helpful!
def suggest(product_idea):
if len(product_idea) <= len(3):
raise ValueError('Sorry, that's too short!')
return product_idea + "inator"
Sander Stantsits
1,773 PointsThe correct answer should be.
def suggest(product_idea): if len(product_idea) <= 3: raise ValueError return product_idea + "inator"
Hope this helps.