Always remember to import modules at start of the script
You code doesn't even run, randrange
isn't defined, it will always throws an NameError
and terminate the execution abruptly, and whatever code comes after it won't run.
You should add this line at the start of the script:
from random import randrange
Always remember to import at the start of scripts, otherwise the names won't get defined and your script won't function properly.
Make your function accept arguments
Make your function accept arguments, make your function accept arguments, make your function accept arguments...
Functions are blocks of code that transforms inputs using predefined procedures into certain outputs; functions make these procedures easier to reuse. Your function doesn't accept any argument at all, and start_time_input
and end_time_input
definitely shouldn't be fixed...
And they should be randomized, as your function name suggests...
Use more functions
Instead of one gigantic function, you should divide the steps into parts and write a smaller function for each part. In this way you will have huge improvements in readability and it will make debugging, testing, profiling etc. a LOT easier.
Always validate user input
Your code contains no input validation because your code accepts no user input... Well your code definitely should accept user input and no input validation will very possibly break the code. Always expect the users to be absolutely computer illiterate and prepare for all possibilities of user input.
Use simpler logic
Seriously, your script is extremely inefficient and poorly writtenl the logic presented by your code could be expressed in far fewer lines...
First, time in the format of hh:mm:ss
is a 3-digit base-60 number in the range [0, 86400) with a colon ':'
as delimiter of the digit fields and the digits are expressed in decimal.
The hour is a number in [0, 23], while minute and second are both in [0, 59], and the hour has a place value of 3600 (60²), the minute 60 and the second 1...
In short, to compare them, just convert them to integers and compare the integers:
h, m, s = t.split(':')
seconds = h * 3600 + m * 60 + s
To format an int
into time stamp:
h, r = divmod(n, 3600)
m, s = divmod(r, 60)
time = f'{h:02d}:{m:02d}:{s:02d}'
And if the start is greater than end, just add 86400 to the end, and always mod 86400 the choice before format, problem solved...
Suggested code:
import re
import sys
from random import randrange
timeformat = '^(2[0-3]|[01]?\d):([0-5]?\d):([0-5]?\d)$'
def parse_time(t: str) -> int:
if not isinstance(t, str):
raise TypeError('Argument must be str')
if re.match(timeformat, t):
h, m, s = t.split(':')
return h * 3600 + m * 60 + s
else:
raise ValueError('Inputted string is not in a correct format')
def time_string(n: int) -> str:
if not isinstance(n, int):
raise TypeError('Argument must be int')
if 0 <= n < 86400:
h, r = divmod(n, 3600)
m, s = divmod(r, 60)
return f'{h:02d}:{m:02d}:{s:02d}'
else:
raise ValueError('Inputted integer is out of range')
def randtime(start=None, end=None) -> str:
if not start:
start = randrange(86400)
if not end:
end = randrange(86400)
if not isinstance(start, int):
start = parse_time(start)
if not isinstance(end, int):
end = parse_time(end)
if start > end:
end += 86400
chosen = randrange(start, end+1)
return time_string(chosen%86400)
if __name__ == '__main__':
print(randtime(*[int(s) if s.isdigit() else s for s in sys.argv[1:]]))