Question

In: Computer Science

. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If  

. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If   value1   and   value2   are   either   integers   or   strings   containing   only   digits,   cast   value1   and   value2   to   integers   and   compute   their   average.       If   their   average   is   greater   than   50,   return   the   string   “Above   50”   and   if   the   average   is   less   than   50,   return   the   string   “Below   50”.       If   the   average   is   equal   to   50,   return   the   string   “Equal   to   50”,   and   if   value1   or   value2   are   not   integers   or   strings   containing   only   digits,   return   the   string   “Invalid   Input”.       Name   this   function   compareToFifty(value1,   value2).       value1   value2   compareToFifty(value1,   value2)   25   25   “Below   50”   100   “300”   “Above   50”   “45”   “100”   “Above   50”   25   75   “Equal   to   50”   24.5   67   “Invalid   Input”   “hello”   “world”   “Invalid   Input”  

Solutions

Expert Solution

def compareToFifty(value1, value2):

        if type(value1) not in (int, str):
                return 'Invalid Input'

        if type(value2) not in (int, str):
                return 'Invalid Input'

        if type(value1) == str:
                if not all([c.isdigit() for c in value1]):
                        return 'Invalid Input'
                value1 = int(value1)

        if type(value2) == str:
                if not all([c.isdigit() for c in value2]):
                        return 'Invalid Input'
                value2 = int(value2)

        avg = (value1 + value2)/2.0

        if avg > 50:
                return 'Above 50'
        if avg < 50:
                return 'Below 50'
        return 'Equal to 50'

print(compareToFifty(25, 25))
print(compareToFifty(100, "300"))
print(compareToFifty("45", "100"))
print(compareToFifty(25, 75))
print(compareToFifty(24.5, 67))
print(compareToFifty("hello", "world"))
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Write a function sublist that takes two lists as arguments, and returns true if the first...
Write a function sublist that takes two lists as arguments, and returns true if the first list appears as a contiguous sublist somewhere within the second list, and false otherwise. > (sublist ’(c d e) ’(a b c d e f g)) #t > (sublist ’(a c e) ’(a b c d e f g)) #f Write a function lgrep that returns the “lines” within a list that contain a given sublist. Use the sublist function implemented in previous exercise...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write a boolean function named isMember that takes two arguments: an array of type char and...
Write a boolean function named isMember that takes two arguments: an array of type char and a value. It should return true if the value is found in the array, or false if the value is not found in the array. PLEASE WRITE FULL PROGRAM IN C++ AND USE RECURSION AND DO NOT USE LOOPS
In Python, write a function called user_name that takes two arguments, first_name and last_name and creates...
In Python, write a function called user_name that takes two arguments, first_name and last_name and creates a “user name” consisting of the first three letters of the first name concatenated with the first three letters of the last name.   You can assume ach name is at least three letters long. Return the new user name.    your results should yield something like these test cases. >>> print(user_name("bob", "taft")) bobtaf >>> >>> print(user_name("Ralph", "Waldo")) RalWal >>>
Write a function which takes two words as string arguments and checks whether they are anagrams...
Write a function which takes two words as string arguments and checks whether they are anagrams NB: Please use spider in anaconda(python) and include a picture of the code
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
Write a function called alternate that takes two positive integers, n and m, as input arguments...
Write a function called alternate that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. Each element of the n-by-m output matrix for which the sum of its indices is even is 1. All other elements are zero. For example, here is an example run: >> alternate(4,5) ans = 1 0 1 0 1 0 1 0 1 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT