Sure, here is how you can check a condition while exceptionhandling in Python:
Python
def my_function():
try:
# This line might raise an exception
1 / 0
except ZeroDivisionError as e:
# Check if the number is even
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
In this example, the function my_function() tries to divide 1 by 0. If this division is successful, the
try block will be executed. However, if the division fails, a
ZeroDivisionError exception will be raised and the except block will be executed.
In the except block, we check if the number is even. If the number is even, we print the message "The number is even." Otherwise, we print the message "The number is odd."
Here is another example:
Python
def my_function():
try:
# This line might raise an exception
num = int(input("Enter a number: "))
except ValueError as e:
print("Invalid input. Please enter a valid number.")
else:
# Check if the number is greater than 10
if num > 10:
print("The number is greater than 10.")
else:
print("The number is less than or equal to 10.")
In this example, the function my_function() asks the user to enter a number. The
try block tries to convert the user input to an integer. If the conversion is successful, the
try block will be executed. However, if the conversion fails, a
ValueError exception will be raised and the except block will be executed.
In the except block, we print the message "Invalid input. Please enter a valid number." Otherwise, we check if the number is greater than 10. If the number is greater than 10, we print the message "The number is greater than 10." Otherwise, we print the message “The number is less than or equal to 10.”
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is how you can check a condition while exception handling in Python:
Python
In this example, the function
my_function()tries to divide 1 by 0. If this division is successful, thetryblock will be executed. However, if the division fails, aZeroDivisionErrorexception will be raised and theexceptblock will be executed.In the
exceptblock, we check if the number is even. If the number is even, we print the message "The number is even." Otherwise, we print the message "The number is odd."Here is another example:
Python
In this example, the function
my_function()asks the user to enter a number. Thetryblock tries to convert the user input to an integer. If the conversion is successful, thetryblock will be executed. However, if the conversion fails, aValueErrorexception will be raised and theexceptblock will be executed.In the
exceptblock, we print the message "Invalid input. Please enter a valid number." Otherwise, we check if the number is greater than 10. If the number is greater than 10, we print the message "The number is greater than 10." Otherwise, we print the message “The number is less than or equal to 10.”