Can anybody give a simple example of ValueError? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anybody give a simple example of ValueError?

9th Jul 2018, 7:14 AM
aaron_He
1 Answer
+ 3
From the Py docs: "... Raised 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... " As I understand it, an instance of ValueError would be, to pass a negative integer to a function which accepts integers, but requires positive integers to work properly, e.g. math.factorial(). https://www.google.com/amp/s/www.geeksforgeeks.org/factorial-in-JUMP_LINK__&&__python__&&__JUMP_LINK/amp/ math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.
9th Jul 2018, 7:44 AM
Hatsy Rei
Hatsy Rei - avatar