hi ... im trying to write a function that tells you if the time you called is a correct military time .... heres what i have so far

def valid_mil_time(mtime):
    if mtime <= 2359 :
        print ("true")
    else:
        print ("false")

but if i was to call it, it looks like this
>>> from valid_mil_time(1299)
true
but i know this isnt true because there is no such time 1299 ...im tryn to get python to say for and example, if mtime is between 1200 and 1259 print true .... for each hour like..... 0000 and 0059, 0100 0159, ...sorry if im not explaining it correctly
please help!

I think you have to define somewere what is correct military time. Currently you ask if the number 1299 is less than 2359 which it is. Maybe something like this:

h = range(0,24)
m = range (0,60)
def valid_mtime(mtime):
        if mtime[0] in h and mtime[1] in m:print 'true'
        else: print 'false'

valid_mtime((12,99))

Edited 6 Years Ago by __qwerty__: n/a

Your military time has to be a 4 character numeric string of format hhmm. You can use function zfill() to pad with the proper amount of leading zeros. To extract the numbers for hh and mm respectively use string slicing.

You should let your function return True or False to make it useful in validating the mtime string.

Edited 6 Years Ago by vegaseat: amount

Here is a solution with reglular expression you can look at.

import re
def valid_mil_time(mtime):
    if re.match(r"(?:([01]\d|2[0-3]):?[0-5]\d)\Z",mtime,re.IGNORECASE):
        print ("true")
    else:
        print ("false")        

'''Out-->
valid_mil_time('0059') 
true

valid_mil_time('0060') 
false

valid_mil_time('2359') 
true

valid_mil_time('2459') 
false
'''

I think you have to define somewere what is correct military time. Currently you ask if the number 1299 is less than 2359 which it is. Maybe something like this:

h = range(0,24)
m = range (0,60)
def valid_mtime(mtime):
        if mtime[0] in h and mtime[1] in m:print 'true'
        else: print 'false'

valid_mtime((12,99))

hey thanks for replying! but im not sure if i understand what this line of code is saying ....if mtime[0] in h and mtime[1] in m: ... can you explain a little?

Use IDLE to break it down if you dont understand.

>>> h
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
>>> mtime = '2350'
>>> mtime
'2350'

>>> mtime[0]
'2'
>>> mtime[1]
'3'
>>> if mtime[0] in h:
	print 'true'
	
>>> if int(mtime[0]) in h:
	print 'true'	
true

>>> len(mtime)
4

>>> #Here you see one problem in __qwerty__ code have to change to int.
>>> #Read about and/or(not(Boolean Operations) how it work

So a little change to get it to work.

h1 = range(0,3)
h2 = range (0,4)
m1 = range(0,6)
m2 = range(0,10)
mtime_range = (0,4)

def valid_mtime(mtime):
    if len(mtime) not in mtime_range:
        print 'Not correct lenght'        
        return
    if (int(mtime[0]) in h1 and int(mtime[1]) in h2) and (int(mtime[2]) in m1 and int(mtime[3]) in m2):
        print 'true'
    else:
        print 'false'

'''Out-->
valid_mtime('0059')
true

valid_mtime('0060')
false

valid_mtime('24')
Not correct lenght
'''

Edited 6 Years Ago by snippsat: n/a

Use IDLE to break it down if you dont understand.

>>> h
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
>>> mtime = '2350'
>>> mtime
'2350'

>>> mtime[0]
'2'
>>> mtime[1]
'3'
>>> if mtime[0] in h:
	print 'true'
	
>>> if int(mtime[0]) in h:
	print 'true'	
true

>>> len(mtime)
4

>>> #Here you see one problem in __qwerty__ code have to change to int.
>>> #Read about and/or(not(Boolean Operations) how it work

So a little change to get it to work.

h1 = range(0,3)
h2 = range (0,4)
m1 = range(0,6)
m2 = range(0,10)
mtime_range = (0,4)

def valid_mtime(mtime):
    if len(mtime) not in mtime_range:
        print 'Not correct lenght'        
        return
    if (int(mtime[0]) in h1 and int(mtime[1]) in h2) and (int(mtime[2]) in m1 and int(mtime[3]) in m2):
        print 'true'
    else:
        print 'false'

'''Out-->
valid_mtime('0059')
true

valid_mtime('0060')
false

valid_mtime('24')
Not correct lenght
'''

when i try ur code and my code this is the error i keep getting

Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
valid_mtime(2378)
File "C:\Python31\valid_mil_time.py", line 6, in valid_mtime
if (int(mtime[0]) in h1 and int(mtime[1]) in h2) and (int(mtime[2]) in m1 and int(mtime[3]) in m2):
TypeError: 'int' object is not subscriptable

hey thanks for replying! but im not sure if i understand what this line of code is saying ....if mtime[0] in h and mtime[1] in m: ... can you explain a little?

mtime is what u send in to the function, in this case (12,99) which is a tuple. mtime[0] is the first index in the tuple and mtime[1] the second therefore; mtime[0] = 12, mtime[1] = 99. h and m are lists which contains the valid hours and minutes.

The if statement asks if mtime[0] is in h and if mtime[1] is in m. In other words if 12 is in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23] and if 99 is in a similar list.

You have to see difference between python 2.x and python 3.x
Most off us will use python 2.x for a copule off year til 3 party moduls is rewritten for python 3.x

Here is code for python 3.x

h2 = range (0,4)
m1 = range(0,6)
m2 = range(0,10)
mtime_range = (0,4)

def valid_mtime(mtime):
    if len(mtime) not in mtime_range:
        print ('Not correct lenght')
        return
    if (int(mtime[0]) in h1 and int(mtime[1]) in h2) and (int(mtime[2]) in m1 and int(mtime[3]) in m2):
        print ('true')
    else:
        print ('false')

The tuple version from __qwerty__ work also good.
You can rewrite as traning for python 3.x

This article has been dead for over six months. Start a new discussion instead.