![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Python - Need help with time card script/wage calculatorHi, 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
|
||||
|
||||
Re: Python - Need help with time card script/wage calculatorHave 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
|
|||
|
|||
Re: Python - Need help with time card script/wage calculatorI 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:
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
|
|||
|
|||
Re: Python - Need help with time card script/wage calculatorQuote:
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
|
|||
|
|||
Re: Python - Need help with time card script/wage calculatorI 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
|
||||
|
||||
Re: Python - Need help with time card script/wage calculatorThanks Dave. That's exactly what I was suggesting.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
Recent GIDBlog
Toyota - 2008 July Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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