Home > front end >  Credit system for Pension calculator (Python3)
Credit system for Pension calculator (Python3)

Time:09-12

A Pension in simplest terms is an amount of money paid to an employee agreed upon from an employer based on how long they worked.

The code I have below is the code based off input data (Year Start Employment, Month Start, day Start) the same goes for the date the year ended.

Then I have a loop that goes through every single year and counts the amount of days you worked for that year between both dates.

Let us say for this EXAMPLE: 

    -The Date Started: January 2, 2020 
    -The Date Ended:   June 23,2023
    from datetime import timedelta, date

    #For the Input Information  

    #Beginning Dates
    print("===============================START WORK DATE INFO======================================\n")
    YearStartEMP  = int(input("Please Enter The Year Employment Started:"))
    MonthStartEMP = int(input("Please Enter The Month Employment Started:"))
    StartDayEmp   = int(input("Please Enter The Day # Employment Started:"))

    #End of Work dates
    print("===============================END WORK DATE INFO======================================\n")  
    YearEndEMP    = int(input("Please Enter The Year Employment Ended:"))
    MonthEndEMP   = int(input("Please Enter The Month Employment Ended:"))
    EndDayEmp     = int(input("Please Enter The Day#  Employment Ended:"))

    print("===============================DAYS PER YEAR WORKED=======================================\n")
    #dates entered 
    StartWork = date(YearStartEMP, MonthStartEMP, StartDayEmp)
    EndWork = date(YearEndEMP, MonthEndEMP, EndDayEmp)   timedelta(days=1)

    #While loop to look through each year and count the days per year for each year betwwen dates
    while StartWork < EndWork:
        next_year = StartWork.replace(year=StartWork.year   1, month=1, day=1)
        if next_year < EndWork:
            diff = next_year - StartWork
        else:
            diff = EndWork - StartWork
        print(f'{StartWork.year}: {diff.days} days')
        StartWork = next_year

This is what the Output would look like off the information I have given.

    ===============================START WORK DATE INFO=======================================

    Please Enter The Year Employment Started:2020
    Please Enter The Month Employment Started:1
    Please Enter The Day # Employment Started:2
    ===============================END WORK DATE INFO=========================================

    Please Enter The Year Employment Ended:2023
    Please Enter The Month Employment Ended:6
    Please Enter The Day#  Employment Ended:23
    ===============================DAYS PER YEAR WORKED=======================================

    2020: 365 days
    2021: 365 days
    2022: 365 days
    2023: 174 days

The question I have is what code or function should I use for the part of code under "===DAYS PER YEAR WORKED===" to make the output look like this. what kind of if statements or functions should I use ASSUMING that a retiree only needs 180 days to get the credit for the year. It should look something like this.

    2020: 365 days, 1 credit
    2021: 365 days, 1 Credit 
    2022: 365 days, 1 Credit 
    2023: 174 days, 0 Credit 

or something like this:

    2020: 365 days 
    2021: 365 days  
    2022: 365 days  
    2023: 174 days  
    
    2020:  1 credit
    2021:  1 Credit 
    2022:  1 Credit 
    2023:  0 Credit       

I've tried doing this but it resulted in an error:

    if DateBracket >= 180:
        Credit = 1
        print("1 credit") 
    elif DateBracket < 180:
        Credit = 0
        print("0 credit")

CodePudding user response:

Here's your working code. I added some comments to explain the changes I made:

from datetime import timedelta, date

# For the Input Information  

# Beginning Dates
print("===============================START WORK DATE INFO======================================\n")
# Added the `.strip()` method to remove whitespaces from the input
YearStartEMP  = int(input("Please Enter The Year Employment Started: ").strip())
MonthStartEMP = int(input("Please Enter The Month Employment Started: ").strip())
StartDayEmp   = int(input("Please Enter The Day # Employment Started: ").strip())

# End of Work dates
print("===============================END WORK DATE INFO======================================\n")  
YearEndEMP    = int(input("Please Enter The Year Employment Ended: ").strip())
MonthEndEMP   = int(input("Please Enter The Month Employment Ended: ").strip())
EndDayEmp     = int(input("Please Enter The Day # Employment Ended: ").strip())

print("===============================DAYS PER YEAR WORKED=======================================\n")

# Dates entered 
StartWork = date(YearStartEMP, MonthStartEMP, StartDayEmp)
EndWork = date(YearEndEMP, MonthEndEMP, EndDayEmp)   timedelta(days=1)

# Also keep track of the total number of credits for simplicity
total_credits = 0

# While loop to look through each year and count the days per year for each year betwwen dates
while StartWork < EndWork:

    next_year = StartWork.replace(year=StartWork.year   1, month=1, day=1)

    if next_year < EndWork:
        # We only need the number of days worked in this year, not the whole timedelta
        days_per_year = (next_year - StartWork).days
    else:
        days_per_year = (EndWork - StartWork).days
    
    # Set `credits` to 1 if the employee worked at least 180 days in the year
    # Else, set `credits` to 0
    credits = 1 if days_per_year >= 180 else 0

    # The code above is equivalent to the following, but it's cleaner
    """
        if days_per_year >= 180:
            credits = 1
        else:
            credits = 0
    """

    # Also print the credits for the year
    print(f'{StartWork.year}: {days_per_year} days, {credits} credit(s)')

    StartWork = next_year
    # Update the total number of credits
    total_credits  = credits


print(f'Total credits: {total_credits}')

And here's the sample output:

===============================START WORK DATE INFO======================================

Please Enter The Year Employment Started: 2020
Please Enter The Month Employment Started: 1
Please Enter The Day # Employment Started: 2
===============================END WORK DATE INFO======================================

Please Enter The Year Employment Ended: 2023
Please Enter The Month Employment Ended: 6
Please Enter The Day # Employment Ended: 23
===============================DAYS PER YEAR WORKED=======================================

2020: 365 days, 1 credit(s)
2021: 365 days, 1 credit(s)
2022: 365 days, 1 credit(s)
2023: 174 days, 0 credit(s)
Total credits: 3
  • Related