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 trialLiam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsIn Python, what's the difference between a Type Error and a Value Error?
I don't know what the difference is between these two, so I just wanted to ask.
2 Answers
Jennifer Nordell
Treehouse TeacherLiam Hayes Hi! I have a note about Steven Parker's example (especially the second one). When I was trying to wrap my head around this, I could not for the life of me understand why the built-in function for changing something to an int would give a ValueError with a string as opposed to a TypeError. Surely, it's expecting a number, right?
Well, no. It isn't. The int()
function takes either a number or a string. It can convert the string "21" to the integer 21, but it can't convert "dog" to a number. Thus, it's getting the right type, but it's still not a value that it can convert. This is when we get the ValueError
. It's of the right type, but the value is such that it still can't be done.
That being said, if we tried to send in something that was neither a string nor a number, it would have generated a TypeError
as it is of the wrong type.
Hope this helps!
edited for additional note In your own custom made functions, you can pass in whatever you want, although ideally, you know what you're passing in. This is not true of built-in functions in Python. When in doubt, check the documentation for the function in question.
Steven Parker
229,670 PointsA TypeError occurs when an operation or function is applied to an object of inappropriate type.
A ValueError occurs when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError
.
Examples: passing arguments of the wrong type (e.g. passing a list
when an int
is expected) should result in a TypeError
, but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError
.
For more details, see the Exceptions page of the Python documentation.
Liam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsThanks for answering Steven.
Hey could you maybe explain again but in like simpler words?
So you're saying if I pass a list when an int is expected, that's a TypeError. But how is something expected in a function in Python? Can't you pretty much pass whatever you want in Python?
If you could give me a couple more examples but written a bit less "text booky", that would be super awesome.
Steven Parker
229,670 PointsOk, here's a couple of really easy examples:
len(42) # this causes a TypeError, because a number is the wrong type for the function
int("dog") # this causes a ValueError, because "dog" cannot be converted to an int value
Liam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsAwesome, thank you very much bro. I appreciate your help a lot :)
Liam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsLiam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsSuper helpful, thank you very much Jennifer :)
Yiyang Tang
2,655 PointsYiyang Tang
2,655 Pointsthat's just super clear!