[Tutor] how to write a function

bhaaluu bhaaluu at gmail.com
Mon Mar 24 15:16:49 CET 2008


On Mon, Mar 24, 2008 at 8:07 AM, Bartruff, Pamela J.
<Pamela.J.Bartruff at supervalu.com> wrote:
>
> Hello Python users,
>
> I am very new to Python, how do I program that converts 24 hour time to 12
> hour time?  The program should have three functions(input, conversion and
> output function)
>
> Thanks for any help
>
> Pamela Bartruff
> _______________________________________________
>  Tutor maillist  -  Tutor at python.org
>  http://mail.python.org/mailman/listinfo/tutor

You define a function with: def functionName():
The body of the function should be indented (4 spaces is good).
So here are examples of 3 'stub' functions that don't do anything (yet):

def input():
    # comments go after a hash
    # this is the input function
    varName = raw_input("Enter input: ")
    return varName

def conversion(varName):
    #  this is the conversion function
    # do something here
    int(varName)
    return varName

def output(varName):
    # this is the output function
    print ("You input %s" % varName)

def main():
    varName = input()
    conversion(varName)
    output(varName)

if __name__ == "__main__":
    main()

You'll have to fill in the parts specific to your problem.
The best way to start is to work the problem out on paper
first: ie. you should already know how to do the math to
convert from 24 hour time, to 12 hour time. If you can't
do it on paper yet, you need to start by learning how to do it
on paper before you can teach the computer how to do it.

Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
"You assist an evil system most effectively by obeying its
orders and decrees. An evil system never deserves such
allegiance. Allegiance to it means partaking of the evil.
A good person will resist an evil system with his or her
whole soul." [Mahatma Gandhi]


More information about the Tutor mailing list