In: Computer Science
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string.
Most pass the following assertions:
assert determine_data_type('1.2') == float
assert determine_data_type('4') == int
assert determine_data_type('EAS503') == str
CODE:-
def determine_data_type(s):
try:
if '.' in s: # if input contains '.' , it means it is float
n = float(s) # convert it into float, if it fails to convert that
means it is string
print("float") # print
else:
n = int(s) # convert it into int, if it fails to convert that means
it is string
print("int") # print
except ValueError:
print("str")
s = input("Enter: ")
determine_data_type(s)
OUTPUT:-
NOTE:- If you have any doubts, please comment below. THANK YOU!!! Please like my answer.