A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

ValueError Exception in Python with Example
Q:

ValueError Exception in Python with Example

belongs to collection: Python Exception Handling Programs

0

Python programming language provides programmers a huge number of exception handler libraries that help them to handle different types of exceptions.

One such exception is ValueError exception, let's understand it in detail.

ValueError Exception in python occurs when the variable is fed with wrong value or the value which is out of the expected bound. This is similar to type error which looks for wrong type instead.

Here is an example that will help you to learn more about it, suppose you are taking marks of the student as input from users. If the user enters strings it is treated as a typeError but if the user enters a value greater than 100, it is treated as a valueError.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to illustrate ValueError in Python

# Python program to illustrate ValueError Exception 

# Try Block Starts
try:
    # Taking input from the user ... 
    # The input strictly needs to be an integer value...
    num1 = int(input("Enter number 1 : "))
    num2 = int(input("Enter number 2 : "))

    # Adding the two numbers and printing their result.
    numSum = num1 + num2
    print("The sum of the two numbers is ",numSum)
    
# except block 
except ValueError as ex:
    print(ex)

Output:

Run 1: 
Enter number 1 : 10
Enter number 2 : 20
The sum of the two numbers is  30

Run 2: 
Enter number 1 : 32.2
invalid literal for int() with base 10: '32.2'

Explanation:

In the above code, we have created two variables num1 and num2 and taken user input for their values. Both the values entered need to be integer values only. And then we have printed their sum. The exception we have handled here is valueError but we have not put any upper limit to the value of the integer to be entered, and by default you can create an integer as big as you want in python. And hence, if we enter a non-integer value the ValueError exception returns the exception statement.

Example 2:

# Python program to illustrate valueError Exception 
import math

# Try Block Starts
try:
    # Taking input from the user ... 
    # The input needs to be a positive integer value...
    num1 = int(input("Enter number 1 : "))
    
    # finding the square root of the number. 
    # So the number cannot be negative.
    squareRoot = math.sqrt(num1)
    print("The Square root of the number is ",squareRoot)
    
# except block 
except ValueError as ex:
    print(ex)

Output:

Run 1:
Enter number 1 : 4
The Square root of the number is  2.0

Run 2:
Enter number 1 : 12.3
invalid literal for int() with base 10: '12.3'

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to raise an exception... >>
<< IndexError Exception in Python with Example...