Home > Net >  How to calculate the number of days left in a given year without using the datetime module?
How to calculate the number of days left in a given year without using the datetime module?

Time:03-31

I am stuck on trying to get the number of days remaining in a given year.

I have already defined a function that determines whether the year is a leap year and how many days in a given month. However, I'm stuck a defining the last function without using the datetime module.

year = int(input("Enter the year to determine the number of days: "))
month = int(input("Enter the month of the year: "))
day = int(input("Enter the day of the year: "))

def if_leap_year(year):

    if (year % 400 == 0): return 366
    
    elif (year % 100 == 0): return 365
    
    elif (year % 4 == 0): return 366

    else:
        return 365

print(if_leap_year(year))

def days_in_month(month, year):
    if month in {1, 3, 5, 7, 8, 10, 12}:
        return 31
    if month == 2:
        if if_leap_year(year):
            return 29
        return 28
    return 30

print(days_in_month(month, year))


def days_left_in_year(month, day, year):
    month2 = 12
    day2 = 31
    day = day2 -365

I am stuck at this very last part... any help would be appreciated.

Edit: So now that I have:

    def days_left_in_year(month, day, year):
        daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
        daysLeft = (if_leap_year(year) if month < 3 else 365) - sum(daysInMonth[:month - 1]) - day
        return daysLeft

I want to use all of these 3 functions to create the output similar to:

When did you cancel your Subprime service?

Month: 2

Day: 29

Year: 2021

Sorry, 2/29/2021 doesn’t appear to be a valid date. Care to try again?

Month: 2

Day: 29

Year: 2020

I tried using:

def refund_period():
    year = int(input("Enter the year to determine the number of days: "))
    month = int(input("Enter the month of the year: "))
    day = int(input("Enter the day of the year: "))
    if if_leap_year(year) == 365:
        money_owed = (days_left_in_year(month, day, year) / 365) * 278
    if if_leap_year(year) == 366:
        money_owed = (days_left_in_year(month, day, year) / 366) * 278

But money_owed is not called, so now I am stuck yet again.... :(

CodePudding user response:

Here's a minimally modified version of your code that does what you've asked:

year = int(input("Enter the year to determine the number of days: "))
#month = int(input("Enter the month of the year: "))
#day = int(input("Enter the day of the year: "))

def if_leap_year(year):

    if (year % 400 == 0): return 366
    
    elif (year % 100 == 0): return 365
    
    elif (year % 4 == 0): return 366

    else:
        return 365

#print(if_leap_year(year))

def days_in_month(month, year):
    if month in {1, 3, 5, 7, 8, 10, 12}:
        return 31
    if month == 2:
        if if_leap_year(year):
            return 29
        return 28
    return 30

#print(days_in_month(month, year))

def days_left_in_year(month, day, year):
    daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
    daysLeft = (if_leap_year(year) if month < 3 else 365) - sum(daysInMonth[:month - 1]) - day
    return daysLeft

print(days_left_in_year(1,1,2020))
print(days_left_in_year(3,1,2020))
print(days_left_in_year(1,1,2022))
print(days_left_in_year(3,1,2022))

Output:

365
305
364
305

UPDATE: I think you're just looking to do this for the second part of your question:

def refund_period():
    year = int(input("Enter the year to determine the number of days: "))
    month = int(input("Enter the month of the year: "))
    day = int(input("Enter the day of the year: "))
    if if_leap_year(year) == 365:
        money_owed = (days_left_in_year(month, day, year) / 365) * 278
    if if_leap_year(year) == 366:
        money_owed = (days_left_in_year(month, day, year) / 366) * 278
    return money_owed

print(refund_period())

Your function refund_period() was not returning anything, so I added return money_owed, then I added the line print(refund_period()).

Here's a sample run:

Enter the year to determine the number of days: 2020
Enter the month of the year: 02
Enter the day of the year: 29
232.42622950819674
  • Related