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
Steven Banks
1,595 PointsRaise a ValueError code not working please HELP!!
I am definitely over looking something very basic here can anyone shed any light on why i keep getting a NameError on this code when am trying to raise a ValueError.
Thanks in advance
def suggest(product_name): return product_name + "inator"
if product_name <= 3: raise ValueError ("Please use more than 3 characters")
suggest("nam")
5 Answers
Louise St. Germain
19,425 PointsHi Steven,
Assuming that the code you posted above looks like this in your editor (see the Markdown Cheatsheet link below the forum post text boxes to see how you can do this - it's easy!):
def suggest(product_name):
return product_name + "inator"
if product_name <= 3:
raise ValueError ("Please use more than 3 characters")
suggest("nam")
The first problem is that your if statement needs to be inside the function definition. The idea is that within the suggest function, you need it to make a choice of what to do, based on the input.
- Choice 1: if the product_name is more than 3 letters long, return the name + "inator".
- Choice 2: otherwise, raise a ValueError.
Because that "if" statement is not part of the function, product_name means nothing at that point. Not only is Python running the if statement before it runs the suggest function, but because product_name only has a value inside the suggest function, product_name is not defined outside the function (we can say it is 'out of scope'). This is why you're getting a NameError.
Also, right now you're comparing product_name (a string) to a number, where what you really want is to compare the string's length to a number - so you'll want to do this:
# Do this...
if len(product_name) <= 3:
# ... not this:
if product_name <= 3:
Finally, you'll want that return product_name + "inator" line last in the function. If you put it first, where it is now, it will run that line and exit out of the function before it ever gets to your if statement.
So in summary, to fix this code, try the following:
- Move the if statement inside the suggest function.
- Move that return statement so it's the last thing in the function (i.e., it needs to evaluate the if statement first).
- Make sure the if statement is comparing the length of product_name to 3.
Hope this helps!
Steven Banks
1,595 PointsThank you both so much for your responses, this has been driving me mad. I have changed the code and i now get the value error. However just out of curiosity if i run it like this
def suggest(product_name):
if len(product_name) <= 3:
raise ValueError ("Please use more than 3 characters")
return product_name + "inator"
suggest("name")
Should this now not return the name - nameinator because when I run it in the workspace nothing happens at all?
Louise St. Germain
19,425 PointsHi Steven,
You just need to remove one level of indent on your return statement so it's not part of the if statement. Right now, if the length is too short, it raises the ValueError (yay!) but then the program stops there. But if the length is OK and you want to return the "inator" value, you want it to skip the stuff inside the if statement (i.e., don't throw an error), and then move to the next non-if-statement line, which should be your return. Like this:
def suggest(product_name):
if len(product_name) <= 3:
raise ValueError ("Please use more than 3 characters")
# Make sure this is not indented inside your if statement:
return product_name + "inator"
suggest("name")
Also, ditto what Kris said - add a print statement so you can see what it's returning. :-)
csr13
33,293 Pointsnp
KRIS NIKOLAISEN
54,974 Pointstry
print(suggest("name"))
Steven Banks
1,595 PointsThanks again. So just to clarify is that because a return value isnt always visible and to see the result of a return i should print it?
Louise St. Germain
19,425 PointsRight - it won't print it to the console (where you can see it), unless you tell it to.
Steven Banks
1,595 PointsBrill thank you again for taking the time to help me.
Louise St. Germain
19,425 PointsNo problem - glad to help!
csr13
33,293 Pointscsr13
33,293 PointsI am assuming that your code reads like this, because that is what it looks like, if you surround code with three of these
`you can write snippets, which makes it easier for the community to read your code.But anyways here is my interpretation of you code:
Two things, the first line is returning the value and not checking if the conditions are true, because return, in this case, needs to be below the condition.
Another thing, do not put a whitespace between ValueError and the ("Please use more than 3 characters") message.
do this instead with no whitespace:
raise ValueError("Please use more than 3 characters.")