I am new to Python, and am confused by the literature as to how to design a function to display error messages when the input is either not a string of length 21. This is the latest permutation that is not working. For a string of length 19 for example, it does not return the right error message. My background is not in comp sci, so any help on this and an explanation as to how this works would be helpful. Thanks
test = "test_string_for_slice"
test2 = "test_string_for_"
def stringSlice(x):
if type(x) != str:
raise Exception("The input is not a string.")
elif len(x) != 21:
raise ValueError("The input is not exactly 21 characters.")
else:
slice1 = x[0:6]
slice2 = x[6:12]
slice3 = x[13:]
return slice1, slice2, slice3
stringSlice(test)
stringSlice(test2) #this should return an error but does not
occCode?occCodetoxwhich is the parameter the function is provided with.