In: Computer Science
. 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”
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.