This Python program reads an integer between 1 and 365 from the user, representing the day of the year in the Persian calendar.
It then calculates which season, month, and day of the month that day corresponds to.
The logic uses a list of months and their lengths, along with a mapping of months to seasons.
def get_date_info(day_of_year):
months = [
("Farvardin", 31), ("Ordibehesht", 31), ("Khordad", 31),
("Tir", 31), ("Mordad", 31), ("Shahrivar", 31),
("Mehr", 30), ("Aban", 30), ("Azar", 30),
("Dey", 30), ("Bahman", 30), ("Esfand", 29)
]
seasons = {
"Farvardin": "Spring", "Ordibehesht": "Spring", "Khordad": "Spring",
"Tir": "Summer", "Mordad": "Summer", "Shahrivar": "Summer",
"Mehr": "Autumn", "Aban": "Autumn", "Azar": "Autumn",
"Dey": "Winter", "Bahman": "Winter", "Esfand": "Winter"
}
remaining = day_of_year
for name, length in months:
if remaining <= length:
season = seasons[name]
return season, name, remaining
remaining -= length
return None, None, None # Invalid input
# Run the program
day = int(input("Enter day of the year (1–365): "))
season, month, day_in_month = get_date_info(day)
if season:
print(f"Day {day} of the year is in {season}, month {month}, and day {day_in_month} of that month.")
else:
print("Invalid day number.")
Day 35 of the year is in Spring, month Ordibehesht, and day 4 of that month.
- The program reads a day number from the user
- It iterates through a list of months and subtracts their lengths until it finds the correct month
- It then uses a dictionary to determine the season based on the month
- The final output includes the season, month name, and day within that month