1

I'm trying to program a salary calculator that tells you what your salary is during sick leave. In Costa Rica, where I live, salaries are paid bi-monthly (the 15th and 30th of each month), and each sick day you get paid 80% of your salary. So, the program asks you what your monthly salary is and then asks you what was the start date and finish date of your sick leave. Finally, it's meant to print out what you got paid each payday between your sick leave. This is what I have so far:

import datetime

salario = float(input("What is your monthly salary? "))

   fecha1 = datetime.strptime(input('Start date of sick leave m/d/y: '), '%m/%d/%Y')

fecha2 = datetime.strptime(input('End date of sick leave m/d/y: '), '%m/%d/%Y')


diasinc = ((fecha2 - fecha1).days)
print ("Number of days in sick leave: ")
print (diasinc)

def daterange(fecha1, fecha2):
    for n in range(int ((fecha2 - fecha1).days)):
        yield fecha1 + timedelta(n)

for single_date in daterange(fecha1, fecha2):
    print (single_date.strftime("%Y-%m-%d")) #This prints out each individual day between those dates.

I know for the salary I just multiply it by .8 to get 80% but how do I get the program to print it out for each pay day?

Thank you in advance.

1 Answers1

0

Here's an old answer to a similar question from about eight years ago: python count days ignoring weekends ...

... read up on the Python: datetime module and adjust Dave Webb's generator expression to count each time the date is on the 15th or the 30th. Here's another example for counting the number of occurrences of Friday on the 13th of any month.

There are fancier ways to shortcut this calculation using modulo arithmetic. But they won't matter unless you're processing millions of these at a time on lower powered hardware and for date ranges spanning months at a time. There may even be a module somewhere that does this sort of thing, more efficiently, for you. But it might be hard for you to validate (test for correctness) as well as being hard to find.

Note that one approach which might be better in the long run would be to use Python: SQLite3 which should be included with the standard libraries of your Python distribution. Use that to generate a reference table of all dates over a broad range (from the founding of your organization until a century from now). You can add a column to that table to note all paydays and use SQL to query that table and select the dates WHERE payday==True AND date BETWEEN .... etc.

There's an example of how to SQLite: Get all dates between dates.

That approach invests some minor coding effort and some storage space into a reference table which can be used efficiently for the foreseeable future.

Community
  • 1
  • 1
Jim Dennis
  • 17,054
  • 13
  • 68
  • 116