Hi, I'm new to these forums. I've been working with java in school, but I can't stand the language. So I've been working with python and find it's very easy to understand, and I actually enjoy working with it.

So just for fun I've been working on a pretty basic program, that converts military time in to standard time and vice versa.

If you guys could answer these questions, it would be awesome, and I would be very grateful.

1) Is it possible to print a time value with a 0 in front of it? (Ex. 0300 hours)
2) Is it possible to print a time value with a colon in it? (Ex. 12:30)
3) Is it possible to be able to enter time values with the colon in it? (Enter a time: 12:30)


Here is the code.

####There might be some errors in the conversions still.  I'm still working on them.
print "This program converts military time to standard time, and vice versa."
print "Enter times as one number. 12:30=1230 1:00==100"
print "Make sure to enter selection exactly how it is show on screen."
print
#x=0
#timecheck=" "
#mn=" "

timecheck=raw_input("Select one (Military Time/Standard Time): ")
if timecheck==("Military Time"):
    x=input("Enter a time: ")
    if x>1200:
        print "Time: ",x-1200,"O'clock"
    if x<1200:
        print "Time: ",x+1200,"O'clock"
if timecheck==("Standard Time"):
    mn=raw_input("(AM or PM): ")
    if mn==("AM"):
        x=input("Enter a time: ")
        if 1200<=x<=1259:
            print "Time: ",x-1200,"Hours"
            
        elif x<1200:
            print "Time: ",x,"Hours"
        elif x>=1200:
            print "Time: ",x+1200,"Hours"
    if mn==("PM"):
        x=input("Enter a time: ")
        if x<=1200:
            print "Time: ", x+1200,"Hours"

Yes, yes, and yes. Welcome to the freedom of Python. :)

First of all, as is typical for Python, there is a module called time (and another one, datetime) that does exactly this stuff automagically. Look them up in the Python docs.

But if you wanted to do it by hand, it might go like this:

def parsetime(timestr):
	if timestr.endswith("PM"):
		offset = 12
		timestr = timestr[:-2]  # strip off 'PM'
	elif timestr.endswith("AM"):
		offset = 0
		timestr = timestr[:-2]
	else:   # assume military time
		offset = 0
	if ":" in timestr:
		hr, minute = timestr.split(":")
		if hr == "12":
			hr = "0"
	else:   # military time
		minute = timestr[-2:]
		hr = timestr[:-2]
	return int(hr) + int(minute)/60. + offset

def printtime(time, military=False):
	time = time % 24
	if military:
		hr = "00%d" % (int(time))
		minute = "00%d" % (int((time % 1) * 60 + 0.5))
		return hr[-2:]+minute[-2:]
	else:
		hr = "00%d" % (int(time) % 12)
		if int(time) % 12 == 0:
			hr = "12"
		minute = "00%d" % (int((time % 1) * 60 + 0.5))
		if int(time) > 11:
			offset = "PM"
		else:
			offset = "AM"
		return hr[-2:] + ":" + minute[-2:] + offset

These aren't perfect (as in, they allow illegal times), but they give an idea as well as showcasing some differences between Python and Java.

Jeff

Wow... My program is so basic looking compared to yours, haha. To be honest I don't know what all the things in your program does.

So if I wanted to get the program to be able to have the : in between the hour and minute. I would need to make separate variables. One to handle the hour and one to hand the minute. Then I would need to have python print it out sort of like this.?

print hour,":",minute


Oh and do you know of any basic programs that I could code to help me learn python?

I would do this:

print hour + ":" + minute

The , character in a print statement means "print on the same line and insert a space."

By contrast, the + operator on strings joins them together (whether in a print statement or otherwise).

Try here for some tutorials/exercises.

http://openbookproject.net//thinkCSpy/

And I'll be happy to explain things from the code above.

Jeff

With Python you can use the slicing operator to help out:

t1 = '1230'  # --> 12:30
t2 = '100'   # --> 1:00

# slicing operator seq[begin : end : step]
# step is optional
# defaults are index begin=0, index end=len(seq)-1, step=1
# -begin or -end --> count from the end backwards
# step = -1 reverses sequence
hr1 = t1[:-2]
mn1 = t1[-2:]
print hr1, mn1  # test --> 12 30


hr2 = t2[:-2]
mn2 = t2[-2:]
print hr2, mn2  # test -->  1 00
This question has already been answered. Start a new discussion instead.