I am not sure how to resolve the date difference calculation in my short program

Viewed 34

'''This project will determine the following''' '''1. Is a loan active in a defined period''' '''2. If a loan is active in a defined period, how many days was it active'''

'''Define the Custom Periods'''

from datetime import datetime, date, timedelta

fy22_p1_start = date(2021, 9, 26)
fy22_p1_end = date(2021, 10, 30)
fy22_p2_start = date(2021, 10, 31)
fy22_p2_end = date(2021, 11, 27)
fy22_p3_start = date(2021, 11, 28)
fy22_p3_end = date(2021, 12, 25)
fy22_p4_start = date(2022, 12, 26)
fy22_p4_end = date(2022, 1, 29)
fy22_p5_start = date(2022, 1, 30)
fy22_p5_end = date(2022, 2, 26)
fy22_p6_start = date(2022, 2, 27)
fy22_p6_end = date(2022, 3, 26)
fy22_p7_start = date(2022, 3, 27)
fy22_p7_end = date(2022, 4, 30)
fy22_p8_start = date(2022, 5, 1)
fy22_p8_end = date(2022, 5, 28)
fy22_p9_start = date(2022, 5, 29)
fy22_p9_end = date(2022, 6, 25)
fy22_p10_start = date(2022, 6, 26)
fy22_p10_end = date(2022, 7, 30)
fy22_p11_start = date(2022, 7, 31)
fy22_p11_end = date(2022, 8, 27)
fy22_p12_start = date(2022, 8, 28)
fy22_p12_end = date(2022, 9, 24)
fy23_p1_start = date(2022, 9, 25)
fy23_p2_end = date(2022, 10, 29)

'''Get input from user about Loan Start and Loan End dates and convert to dates'''

loan_start = str(input('Loan Start date(yyyy-mm-dd): '))
loan_start = datetime.strptime(loan_start, '%Y-%m-%d').date()

loan_end = str(input('Loan End date(yyyy-mm-dd): '))
loan_end = datetime.strptime(loan_end, '%Y-%m-%d').date()

print('')
print(f'Period Start Date:',fy22_p7_start)
print(f'Period End Date:',fy22_p7_end)
print('')

'''Determine if Loan is in a specific period'''

if loan_start > fy22_p7_end or loan_end < fy22_p7_start:
    in_period = ('Loan was not in Period')
    print(in_period)
else:
    in_period = ('Loan was in Period')
    print(in_period)

'''Calculate Days in Period start and end'''

if loan_start >= fy22_p7_start:
    per_calc_start = loan_start
else:
    per_calc_start = fy22_p7_start

if loan_end <= fy22_p7_end:
    per_calc_end = loan_end
else:
    per_calc_end = fy22_p7_end

'''Calculate Days in Period Days'''

period_days = (per_calc_end - per_calc_start)

if period_days > 0:
    print(f'Your loan was active for', period_days, 'this period')
else:
    print('Your loan was not active in this period')

'''Calculate total loan days'''

days = (loan_end - loan_start)
print(f'Total Loans Days:', days)

'''Here is the output I get''' Loan Start date(yyyy-mm-dd): 2022-09-01 Loan End date(yyyy-mm-dd): 2022-09-15

Period Start Date: 2022-03-27 Period End Date: 2022-04-30

Loan was not in Period

'''Here is the error I get:'''

TypeError Traceback (most recent call last) Input In [43], in <cell line: 75>() 71 '''Calculate Days in Period Days''' 73 period_days = (per_calc_end - per_calc_start) ---> 75 if period_days > 0: 76 print(f'Your loan was active for', period_days, 'this period') 77 else:

TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'

0 Answers
Related