Question

In: Computer Science

Write a small section of Python code that defines a function called check_even(value) - the function...

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.

Solutions

Expert Solution

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


Related Solutions

In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should...
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should have #two parameters: a dictionary and a string. The string may be #a key to the dictionary. The value associated with that key, #in turn, may be another key to the dictionary. # #Keep looking up the keys until you reach a key that has no #associated value. Then, return that key. # #For example, imagine if you had the following dictionary. #This one...
In this problem, we'll write a Python module that defines two things publicly. A namedtuple called...
In this problem, we'll write a Python module that defines two things publicly. A namedtuple called Student with two fields, scores and grade, intended to carry grade-related information about a student. (This will be part of the output of this problem.) So that we're all in agreement about what that will look like, here it is: Student = namedtuple('Student', ['scores', 'grade']) A function named build_grade_report, which takes two parameters: a Path object describing the path to a file containing the...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
Write a program in PYTHON which: *Defines the following 5 functions: whoamI() This function prints out...
Write a program in PYTHON which: *Defines the following 5 functions: whoamI() This function prints out your name and PRINTS OUT any programming course you have taken prior to this course. isEven(number) This function accepts one parameter, a number, and PRINTS OUT a message telling if the numberer is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two. printEven(number) This function accepts one parameter, a number,...
code in python write a code where the initial value is compounded by a multiplier, rounded...
code in python write a code where the initial value is compounded by a multiplier, rounded to the nearest tenth Output: Initial Value: 10 Multiplier: 1.4 Number of compounds: 10 Your Values are: 10 , 14, 19.6 , 27.4, 38.4, 53.8, 75.3, 105.4, 147.6, 206.6
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
In python of Jupiter notebook Write a python function called trng that takes three numbers x,...
In python of Jupiter notebook Write a python function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangleotherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified:...
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified: Request input from the user for three variables (floating-point or integer) x, y, z, and myAverage. If the average of the first three numbers equals the fourth number, print 'Your average is correct.'. If not print 'Your average is not correct'. and print the correct average. Print the largest value among x, y, and z. Print the minimum value of x, y, y. >>>...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT