Home > Net >  Why is my process of finding the number of integer years, integer months, and integer days from the
Why is my process of finding the number of integer years, integer months, and integer days from the

Time:12-15

I need to find out the number of integer years, number of integer months, and number of integer days from the number of days, given as an input. However, my process is not working. I am getting the wrong answer.

Note: Assume, each year to be 365 days and each month to be 30 days. Also, please do not consider leap years.

user=int(input("Please enter the number of days: "))

def year(num):
  year=int(num/365)
  month=int((num65)/30)
  day=num0
  print(f"{year} years, {month} months and {day} days")

year(user)

I am getting 11 years, 10 months and 10 days as the output. However, the question prompt also gave me some sample outputs. In their sample output the output for 4330 was 11 years, 10 months and 15 days. Why are we getting different outputs? My code is flawless. I am getting what I expected to get. However, my expected output is not matching with the output given in my prompt.

CodePudding user response:

I'm not sure why you're calculating the number of days using day=int((((num/365-int(num/365))*12)-month)*30), which I can't understand logically. A very simple, readable way would be:

def year(num):
    year=num//365
    month=(num65)//30
    day=num-(year*365)-(month*30)
    print(f"{year} years, {month} months and {day} days")

year(4330) # prints 11 years, 10 months and 15 days

Edit:

day=num0 wouldn't work, because it doesn't factor in the fact that an year has 365 days. Consider num = 730, which is 2 years and 0 days. Using your formula, you'll get day = 10, which is wrong.

  • Related