Problem statement: We are given a number of days, and our task is to convert this value into years, weeks, and remaining days using Python. To solve this, we first take the total number of days as input, then apply simple arithmetic calculations where 1 year = 365 days and 1 week = 7 days. After performing the conversion, the program displays the equivalent years, weeks, and days in a clear format.
This is a straightforward arithmetic problem. Use integer division and the modulo operator to extract years, then weeks from the remaining days, and finally the leftover days.
Steps:
n from the user (number of days).years = n // 365 — integer division gives full years.remaining_after_years = n % 365 — days left after removing years.weeks = remaining_after_years // 7 — full weeks from the remaining days.days = remaining_after_years % 7 — leftover days.This method assumes fixed-length years (365 days). If you need calendar-accurate results across leap years, use datetime and relativedelta from dateutil (not shown here).
Using Basic Arithmetic : This method performs step-by-step calculations using integer division and modulo. It is beginner-friendly because each conversion step is clearly visible and easy to understand.
# Simple program to convert days into years, weeks and days
days_input = int(input("Enter number of days: "))
years = days_input // 365
remaining = days_input % 365
weeks = remaining // 7
days = remaining % 7
print(f"{days_input} days is equal to {years} year(s), {weeks} week(s), and {days} day(s).")
Code language: PHP (php)
Explaination : This simple script uses basic arithmetic and straightforward input/output. It’s ideal for beginners because it explicitly shows each step: calculating years, then weeks, then remaining days. Use this version to quickly test and understand the logic before refactoring into functions. For invalid (negative) inputs, consider adding a guard to prompt the user again.
Output:
Enter number of days: 800
800 days is equal to 2 year(s), 10 week(s), and 5 day(s).
Using Function Approach This method wraps the conversion logic inside a reusable function. It improves code organization and makes the program easier to test and maintain.
def convert_days(total_days: int) -> tuple:
"""Return (years, weeks, days) for a given total number of days."""
if total_days < 0:
raise ValueError("Number of days must be non-negative")
years = total_days // 365
remaining = total_days % 365
weeks = remaining // 7
days = remaining % 7
return years, weeks, days
if __name__ == "__main__":
n = int(input("Enter number of days: "))
y, w, d = convert_days(n)
print(f"{n} days = {y} year(s), {w} week(s), and {d} day(s).")
Code language: PHP (php)
This function-based approach encapsulates the conversion logic so it’s reusable and easy to unit-test. Separating I/O from computation makes automated testing and integration simpler. It also allows handling edge cases (like negative input) centrally. Use this when you plan to call the conversion from other modules or write tests for behaviour.
divmod() for compact codeUsing divmod() Technique This method uses Python’s built-in divmod() to compute quotient and remainder together. It is efficient and concise because it reduces the number of separate arithmetic operations.
n = int(input("Enter number of days: "))
years, rem = divmod(n, 365)
weeks, days = divmod(rem, 7)
print(f"{n} days = {years} year(s), {weeks} week(s), and {days} day(s)")
Code language: PHP (php)
The divmod() version is the most compact and idiomatic Python solution: it performs division and modulo in one step and returns both results as a tuple. This reduces temporary variables and keeps the logic easy to read. It’s a great choice when you want concise, maintainable code without sacrificing clarity. If you’re teaching absolute beginners, you may keep the expanded version with named steps for readability.
All three methods use the same arithmetic idea. Divide by 365 to get complete years; use the remainder to compute weeks (divide by 7) and the final remainder gives leftover days.
Edge cases / notes:
0 returns 0 year(s), 0 week(s), 0 day(s).ValueError).datetime.timedelta or dateutil.relativedelta.divmod() to make the code shorter and clearer.