I just wrote this script that calculates the time difference between two times. It's going to be used in a larger program that will store the returned value in a database.
I'm mainly wondering if I've used docstrings, comments and try/raise/except correctly since it's my first time really using them.
def time_difference(time_start, time_end): '''Calculate the difference between two times on the same date. Args: time_start: the time to use as a starting point time_end: the time to use as an end point Returns: the difference between time_start and time_end. For example: >>> time_difference(1500, 1600) 60 >>> time_difference(1100, 1310) 130 Raises: TypeError: if time_start and time_end is not a string ValueError: if time_start and time_end is below 0 or above 2400 ''' try: if not isinstance(time_start, str): raise TypeError('time_start has to be a string!') elif not isinstance(time_end, str): raise TypeError('time_end has to be a string!') elif int(time_start) > 2400 or int(time_end) < 0: raise ValueError('time_start nor time_end can be below 0 or above 2400!') elif int(time_start) > int(time_end): raise ValueError('time_start cannot be higher than time_end!') except (TypeError, ValueError): raise # Convert time_start and time_end to necessary variables # for the calculation. hours_begin = time_start + time_start minutes_begin = time_start + time_start hours_end = time_end + time_end minutes_end = time_end + time_end # Calculates the difference. minutes = ((int(hours_end) - int(hours_begin)) * 60 + \ int(minutes_end) - int(minutes_begin)) return(minutes)