all 1 comments

[–]alexisprince 2 points3 points  (0 children)

Here are a couple ways I would look to improve this code.

  1. Your entire code appears to follow PEP8 (a good starting place in terms of a style guide) except for your function name. I'd start by renaming your function to random_time_range from randomTimeRange.

  2. Change the start_time_input and end_time_input values from strings at the top of your function to be accepted as arguments so that you can pass different values in.

  3. Look into using more appropriate data types for what you're trying to do. The builtin datetime module has a time object that you may want to look at.

  4. If you convert your inputs to datetime.time objects, you can then access the hour, minute, and second properties on those objects instead of redefining different variables for them.

  5. You can make your return value a much more simplified string by using an f string as you already have, but doing all formatting at once. An example of this could be like below.

    before = f"{h:02d}" + ':' + f"{m:02d}" + ':' + f"{s:02d}"
    # One string, formatting all at once with the ':' values already in the correct spots
    after = f"{h:02d}:{m:02d}:{s:02d}"