Home > Mobile >  Finding Minutes Between Years
Finding Minutes Between Years

Time:11-08

can you please check my code? I think something is wrong somewhere.


import datetime
this_year = datetime.datetime.today().year


def age_in_minutes(b_year: int):
    d1 = datetime.datetime.strptime(f'{this_year}', '%Y')
    d2 = datetime.datetime.strptime(f'{b_year}', '%Y')
    age = (d1-d2).days * 24 * 60
    return f"{age:,}"


while True:
    birth_year = input("Enter you birth year: ")
    try:
        if int(birth_year) == this_year:
            print("Please enter year less than this year...")
        elif int(birth_year) > this_year:
            print("Please enter year less than this year...")
        elif len(birth_year) < 4 or len(birth_year) > 5:
            print("PLease enter a valid year...")
        elif int(birth_year) <= 1900:
            print("Please enter a year after 1900...")
        else:
            minutes_old = age_in_minutes(int(birth_year))
            print(f"You are {minutes_old} minutes old.")
            break
    except ValueError:
        print("Please enter in a year format: ")

I'm tackling a challenge. It said if I enter 1930, I should get "48,355,200". But I'm getting "48,388,320". I'm new to Python.

CodePudding user response:

You haven't calculated leap year. You can follow this:
import datetime

this_year = datetime.datetime.today().year


def age_in_minutes(b_year: int):
    leap_year = 0
    non_leap_year = 0

    for year in range(b_year, this_year 1):
        if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
            leap_year  = 1
        else:
            non_leap_year  = 1
    
    leap_year_in_days = leap_year * 366
    non_leap_year_in_days = non_leap_year * 365
    age = (leap_year_in_days   non_leap_year_in_days) * 24 * 60
    return f"{age:,}"


while True:
    birth_year = input("Enter you birth year: ")
    try:
        if int(birth_year) == this_year:
            print("Please enter year less than this year...")
        elif int(birth_year) > this_year:
            print("Please enter year less than this year...")
        elif len(birth_year) < 4 or len(birth_year) > 5:
            print("PLease enter a valid year...")
        elif int(birth_year) <= 1900:
            print("Please enter a year after 1900...")
        else:
            minutes_old = age_in_minutes(int(birth_year))
            print(f"You are {minutes_old} minutes old.")
            break
    except ValueError:
        print("Please enter in a year format: ")

CodePudding user response:

You could try this code I found it somewhere in 2021 but I don't know the source

current_year = 2022
year = int(input("Please enter a year: "))

if year == current_year:
    print("There are 0 minutes from", year, "to present.")
elif year < current_year:
    print("There are", (current_year - year) * 525600, "minutes from", year, "to present.")
else:
    print("There are", (year - current_year) * 525600, "minutes from present to", year)

CodePudding user response:

try this:

import datetime
this_year = datetime.datetime.today().year

print(this_year)
def age_in_minutes(b_year: int):
    d1 = datetime.datetime.strptime(f'{this_year}', '%Y')
    d2 = datetime.datetime.strptime(f'{b_year}', '%Y')
    age = (d1-d2).days * 24 * 60
    return f"{age:,}"


while True:
    birth_year = int(input("Enter you birth year: "))
    try:
        if birth_year == this_year:
            print("Please enter year less than this year...")
        elif birth_year > this_year:
            print("Please enter year less than this year...")
        elif len(str(birth_year)) < 4 or len(str(birth_year)) > 5:
            print("PLease enter a valid year...")
        elif birth_year <= 1900:
            print("Please enter a year after 1900...")
        else:
            minutes_old = age_in_minutes(birth_year)
            print(f"You are {minutes_old} minutes old.")
            break
    except ValueError:
        print("Please enter in a year format: ")

Output:

Enter you birth year: 1930
You are 48,388,320 minutes old.

I don't do any exceptional but change your type casting, and it can give me a desire result.

  • Related