GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Mar-2008, 17:44
sigkill9 sigkill9 is offline
New Member
 
Join Date: Mar 2008
Posts: 3
sigkill9 is on a distinguished road

Python - Need help with time card script/wage calculator


Hi, I'm trying to build a simple python program that will total how many hours a worker has worked based on a start time and end time entry but the problem is this "simple" problem is becoming very complicated and i'm affraid i'm way off base and digging deeper and deeper into a coding nightmare... Think of it as a sort of time card system if you will.

I'm a beginner python coder (only been at it for 3 months now) if that helps explain my ignorance...

Specifically, I want the terminal operator to enter a start and end time in hh:mm format and have python then determine how many hours and minutes the worker worked based on his/her entries, then calculate the wage before taxes based on a pre-set wage variable. The program has to be able to identify either "am" or "pm" time and do the proper conversions.

Heres an example of the output i'm looking for (based on a $12.50/hr wage):
(red text is user input, blue text is of course python)

>>> Please enter your start time (hh:mm): 12:30pm
>>> Please enter your end time (hh:mm): 6:00pm
>>> Total time worked: 6hrs 30min
>>> Total wages before taxes: $ 81.25

The problem i'm having, however, is figuring out how to make python calculate the correct amount of hours worked based on the hh:mm input from the user. I've done some string conversions, and splicing to isolate the hours and min into seperate variables but i'm geting lost in all the code. I'd like the resulting code to be simple and elegant.

Like, how do you tell python that from 12:30pm to 6:00pm equals 6 hours 30 min? Or from 7:30am to 4:45pm equals 9 hours 15 min and have that value multiplied by the wage?
  #2  
Old 06-Mar-2008, 20:57
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,534
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Python - Need help with time card script/wage calculator


Have you tried converting your time to a different format, e.g. military time (24 hour clock) and/or calculating how many minutes there are between the two times? You don't have to care how the data format is within your code; that only matters when it's output to a user.

If you used a 24 hour clock, you simply subtract the smaller number from the bigger to tell how much time has passed. Converting to minutes eliminates the need to deal with fractions of an hour. If you change the pay to dollars/minute, then you can figure the pay easily.

Then you can simply convert the data you have to whatever format best suits the output desired. This lets you keep the data separate from the presentation, a big concern for programmers since you can create multiple interfaces while the back-end data crunching doesn't change.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 07-Mar-2008, 09:48
sigkill9 sigkill9 is offline
New Member
 
Join Date: Mar 2008
Posts: 3
sigkill9 is on a distinguished road

Re: Python - Need help with time card script/wage calculator


I did try the military time approach, but I'm stuck on how to get python to calculate the correct amount of minutes between the two times.

Here, I'll post the code I have so far below. The problem is it doesn't calculate the final value for actual minutes worked correctly:

Python Code:
def main():

        from math import *
        from string import *

        print "This program calculates the wage earned based on the number of hours worked.\n"

        startTime = raw_input("Enter the start time in hours:min format: ")
        endTime = raw_input("Enter the end time in hours:min format: ")

     #splits time input into usable chunks
        startSplit = split(startTime,":")
        endSplit = split(endTime,":")

     #converts strings to usable integers
        startHrs = int(startSplit[0])
        startMin = int(startSplit[1])
        endHrs = int(endSplit[0])
        endMin = int(endSplit[1])

    #12 hr to 24hr time conversion: 
    #The +12 applied to "startHrs" and "endHrs" assumes all time is PM only
    #I'll worry about the AM, PM logic later, thats simple.
        startTimeConvert = ((startHrs+12)*100)+startMin
        print "The start time conversion is ",startTimeConvert

        endTimeConvert = ((endHrs+12)*100)+endMin
        print "The end time conversion is ",endTimeConvert

        totalTime = endTimeConvert - startTimeConvert
        print "Total time is: ",totalTime

    #use "0"+str if time is only 3 digits. Makes indexing easier!
    #I'll implement the IF statement after I get coding to work correctly.
        totalHrs = "0"+str(totalTime)[0:1]
        totalMin = "0"+str(totalTime)[2:3]
        print "totalHrs test: ",totalHrs
        print "totalMin test: ",totalMin

    #hourly rate earned
        wage = (int(totalHrs)*12.55)+((int(totalMin)*12.55)/60) 
                   #divided min by 60 to get correct wage for minutes portion
        print "Wage test: $",wage

if __name__ == '__main__':
    main()


I plan on dividing this all up into separate functions to streamline the script, but that comes later... after it is actually working correctly.

One of the issues I'm trying to sort out is that if the start time is 1:00pm (1300 military time) and the end time is 6:00pm (1800 military time), if you take 1800 and subtract the 1300 from it, this equals 500. But there are only 300 minutes between 1pm and 6pm. Maybe the solution is so simple that I've completely overlooked it, but does anyone here have any suggestions on how to get this code to work correctly?
Last edited by admin : 08-Mar-2008 at 01:08. Reason: Please insert your example Python codes between [PY] and [/PY] tags
  #4  
Old 07-Mar-2008, 11:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,641
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Python - Need help with time card script/wage calculator


Quote:
Originally Posted by sigkill9
...f the start time is 1:00pm (1300 military time) and the end time is 6:00pm (1800 military time), if you take 1800 and subtract the 1300 from it, this equals 500. But there are only 300 minutes between 1pm and 6pm.

Your analysis proves that you can't just subtract decimal 1300 from decimal 1800 to get the number of minutes, right?


So, why not just convert everything to minutes:

If the time is 13:45 on a 24-hour clock (it's sometimes pronounced "thirteen forty-five hours"), it really means that 13 hours and 45 minutes have elapsed since midnight, right?

How many minutes are there in 13 hours and 45 minutes? (13*60+45, or 825 minutes, right?)

If the time is, say 18:23 ("eighteen twenty-three hours"), how many minutes have elapsed since midnight? (18*60+23, or 1103 minutes, right?)

Then how many minutes from start to finish? (1103-825, or 278 minutes, right?)

Convert 278 minutes to whatever form you need for the formula.

Regards,

Dave
  #5  
Old 07-Mar-2008, 16:06
sigkill9 sigkill9 is offline
New Member
 
Join Date: Mar 2008
Posts: 3
sigkill9 is on a distinguished road

Re: Python - Need help with time card script/wage calculator


I figured it out after a much needed nap!

I converted everything to minutes, that solved the dilemma.

The code works for the most part but it's still a little buggy. More time is needed to analyze it and find the bugs.

Thanks!
  #6  
Old 12-Mar-2008, 21:01
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,534
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Python - Need help with time card script/wage calculator


Thanks Dave. That's exactly what I was suggesting.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes and allocating memory BlueFireCO. CPP / C++ Forum 13 26-Jul-2007 20:31
constructors/classes mapes479 CPP / C++ Forum 3 19-Nov-2006 17:34
Looking for opinions crystalattice Miscellaneous Programming Forum 6 27-Sep-2006 21:02
Need Help with my Cards Program (C++) krisopotamus CPP / C++ Forum 2 06-Oct-2005 16:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.