[Tutor] how to write a function

Marc Tompkins marc.tompkins at gmail.com
Mon Mar 24 18:20:23 CET 2008

On Mon, Mar 24, 2008 at 6:28 AM, Bartruff, Pamela J. <
Pamela.J.Bartruff at supervalu.com> wrote:

> Def convert_time(time):
> """split hours from minutes"
> Time = raw_input("enter a time, in military: ")

At this point, Time is a string, not a number.  (At this point, we also have
to _assume_ that the user entered a valid date string, but you can worry
about validation a little later...)

    hours = Time.split(':')[0]
    minutes = Time.split(':')[1]

The "split" method returns a list of strings.  I've told it to use a colon
(':') as the separator.  "hours" takes the first item in the list - remember
that we start from 0, not from 1 - and "minutes" takes the second item.

At this point you still have two strings, not numbers - convert them to
integers like so:
    hours = int(Time.split(':')[0])
    minutes = int(Time.split(':')[1])
(note - these lines replace what we did above)

And now you can move on to the rest of  your logic...

If hours > 12
>  Hours = hours - 12
>  Am_or_pm = "p.m."
> Output_hours = hours
> Output_minutes = minutes
> Output_hours = ""
> Else:
>  Am_or_pm = "a.m."

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080324/5033ccd7/attachment-0001.htm 

More information about the Tutor mailing list