In: Computer Science
Write a small section of Python code that defines a function called check_even(value) - the function takes a numerical value as input and returns True or False based on whether the provided argument is divisible by 2 (i.e. is it odd or is it even). If the argument provided is not a number (as determined by built-in the isdigit() function - which only works on string data) then rather than crashing, the function should raise an exception which is caught outside the function (i.e. in the block of code calling the function). Provide three calls to the function: - One with an even value where the function should return True, - Another with an odd value where the function should return False, and - One with non-numerical data which should raise an exception that will be caught and displayed in the block of code that calls the function.
Program
#Function to check whether the given number is even or Not
def check_even(value):
if value % 2 != 0:
return False
else:
return True
num=input("Please enter the number\n")
#try block to check if the number entered is or not
try:
num.isdigit()
y=int(num)
output=check_even(y)
print(output)
#exception will occur if string is entered
except ValueError:
print("Please enter the valid number")
Output 1:
Please enter the number
9
False
Output 2::
Please enter the number
10
True
Output 3:
Please enter the number
tetst
Please enter the valid number
Attached is the screenshot of the code for indentation purpose