-2

I want to receive how many years/months/weeks/days in total of the number, I want the output to say 732 is 2 years, 0 months, 0 weeks, 2 days but I would receive "it has been: 2 Years , 24 Months , 105 Weeks , 732 Days Since Variable inputted"

*##variables / user input*

days = int(input("Enter Number of Days: "))
weeks = days / 7
months = days / 30.4
years = days / 365

*#if conditions here*

if days >= 7:
    weeks = weeks + 1
elif weeks >= 4:
    months = months + 1

elif months >= 12:
    years = years + 1





*##print command for output*

print("it has been: ", int(years), "Years", ",", int(months)
      , "Months", ",", int(weeks), "Weeks", ",", int(days), "Days","Since Variable inputted")

I know what I'm doing wrong just I don't know the solution for it :D thank you for any informative answers

D1nky
  • 11
  • 3
  • Start with determining the years, then subtract the days that the full years amount: `days = days - int(years) * 365`. Then you only work with the remaining days to determine the full months, subtract the days of the full months and so on. – Michael Butscher Oct 05 '22 at 22:19
  • Welcome to Stack Overflow. Please read [ask] and note well that this is **not a discussion forum**. Instead of saying "I know what I'm doing wrong just I don't know the solution for it", **explain what is wrong** when the code runs, **explain your understanding** of the cause, and **ask a question** - starting with a question word like "how" or "why", and ending with a question mark. Do not tell us about your level of experience, or anything else outside the **question itself**. – Karl Knechtel Oct 05 '22 at 22:46
  • As for the problem, try to think about the code logically. When the code gives a result that says `24 Months`, where did that come from? It came from accounting for the days that make up the initial `2 years`, right? So - how many days are in `2 years`? What happens if you try *subtracting those days out, after* you reckon the 2 years? – Karl Knechtel Oct 05 '22 at 22:47

2 Answers2

2

There are a lot of ways of going about solving this type of problem. Here is one solution that uses divmod.

days = int(input("Enter Number of Days: "))
years, days = divmod(days, 365)
months, days = divmod(days, 30.4)
weeks, days = divmod(days, 7)

print(f"It has been: {years} years, {months} months, {weeks}, weeks, {days} days,"
      + " since the inputted variable.")

divmod divides two numbers and gives back the quotient and remainder. In the code above we divide the number of days by 365, store the quotient as the number of years, and store the remainder as the number of days — overwriting whatever was previously stored in the days variable with the amount of leftover days. This process is then repeated to calculate months and weeks and days

ruu kes
  • 3
  • 2
hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
  • Thank you for your response, i looked into it and tried 50,000 but started getting wrong results again, my guess would be leap years.. how can this be implimented? – D1nky Oct 05 '22 at 23:53
  • Looks like I was dividing by 30 for the months and you were dividing by 30.4. Perhaps that was the problem. You could also try using 365.2425 to account for leap years. The most exact solution would require you to know what the start and end dates are. Once you know that info you can perform the calculations like so https://stackoverflow.com/questions/50812731/calculate-years-months-days-between-2-dates and the datetime library will automatically take into account which years are leap years and any other oddities with how time is calculated. – hostingutilities.com Oct 06 '22 at 00:24
0

You don't really need any if statements here. This is a more mathematical solution than the one above:

days = int(input("Enter Number of Days: "))
years = days // 365
months = (days - 365 * years) // 30.4
weeks = (days - (365 * years + 30.4 * months))//7
days = days - (365 * years + 30.4 * months + 7 * weeks)
print("it has been: ", int(years), "Years", ",", int(months), "Months", ",", int(weeks), "Weeks", ",", int(days), "Days","Since Variable inputted")

It's just recurring subtraction/division.

Also, when dealing with more complicated projects, it can be useful to explain what you've done to try to fix it, your approach, exactly what you want, and since you said you knew what you were doing wrong, you are expected to say that if you have any information.

OnMyTerms
  • 13
  • 4
  • 2
    @D1nky I thought you wanted 732 to print "2 years, 0 months, 0 weeks, 2 days ". This answer does not do that. – DarrylG Oct 05 '22 at 22:25
  • @DarrylG I'm sorry I just realized I read the question wrong. He was asking for the opposite thing. Mb, I'll try to fix it now – OnMyTerms Oct 05 '22 at 22:28
  • One way to fix it is to use this approach, but include weeks [Python Source Code: Days to Years, Months & Days Conversion](https://www.codesansar.com/python-programming-examples/convert-number-days-years-months-days.htm) It's a little longer than hostingutilities.com approach but same idea. – DarrylG Oct 05 '22 at 22:42