Making a planting calendar

INTRODUCTION

This program helps gardeners know exactly when to plant their flowers. It uses a planting calendar that lists the start and end dates for planting various flowers, including Roses, Tulips, Sunflowers, Lavender, and Lillies. By comparing the current date with the planting season of each flower, the program tells you whether it’s the right time to plant, if you’re too early, or if you’ve missed the window.

When you enter a flower’s name, the program quickly checks its planting season and gives you an answerIf the current date falls within the planting window, it tells you it’s time to plant.If you’re too early, it shows you when you should start.If you’re too late, it tells you when the planting season ended.This tool helps you plan your gardening tasks effectively, so you can always plant at the right time

PROGRAM

import datetime

planting_calendar = {
    "Rose": {"start": (3, 15), "end": (5, 30)},  
    "Tulip": {"start": (9, 1), "end": (11, 15)},    
    "Sun flower": {"start": (4, 1), "end": (6, 30)}, 
    "Lavender": {"start": (5, 1), "end": (7, 15)},   
    "Lilly": {"start": (6, 15), "end": (8, 15)},    
}

def check_planting_time(plant):
    today = datetime.date.today()
    plant = plant.capitalize()  

    if plant in planting_calendar:
        start_month, start_day = planting_calendar[plant]["start"]
        end_month, end_day = planting_calendar[plant]["end"]
        
        start_date = datetime.date(today.year, start_month, start_day)
        end_date = datetime.date(today.year, end_month, end_day)
        if end_date < start_date:
            end_date = datetime.date(today.year + 1, end_month, end_day)

        if start_date <= today <= end_date:
            print(f"It's time to plant {plant}!")
        elif today < start_date:
            print(f"It's too early to plant {plant}. Start planting between {start_date.strftime('%B %d')} and {end_date.strftime('%B %d')}.")
        else:
            print(f"It's too late to plant {plant} this year. The planting season was from {start_date.strftime('%B %d')} to {end_date.strftime('%B %d')}.")
    else:
        print(f"{plant} is not in the planting calendar.")


check_planting_time("Rose")
check_planting_time("Tulip")
check_planting_time("Sun flower")

EXPLANATION OF THE PROGRAM

importing the datetime module

import datetime

creating the Planting_Calendar Dictionary

planting_calendar = {
    "Rose": {"start": (3, 15), "end": (5, 30)},  
    "Tulip": {"start": (9, 1), "end": (11, 15)},    
    "Sun flower": {"start": (4, 1), "end": (6, 30)}, 
    "Lavender": {"start": (5, 1), "end": (7, 15)},   
    "Lilly": {"start": (6, 15), "end": (8, 15)},    
}

A dictionary called planting_calendar is defined.This dictionary contains information about the planting seasons for different types of flowers.

The keys of the dictionary are the names of the flowers.

The values are dictionaries that store the start and end dates for each flower’s planting season.

These dates are represented as tuples of two numbers:

the first is the month and the second is the day.

Defining the check_planting_time Function

def check_planting_time(plant):

This function is responsible for checking if the current data is within the planting window for a given flower.

The function takes one argument,plant,which is the name of the flower the user wants to check.

Getting the current Data

today = datetime.date.today()

inside the function, the variable today is set to the current data using datetime.date.today().

This retrieves the current year,month,and day from the system’s clock,which will later be compared with the flower’s planting season.

Normalizing the plant name

plant = plant.capitalize()

To ensure consistency when looking up the plant name in the planting_calendar dictionary,the program capitalizes the first letter of the input string.This handles cases where the user might enter the plant name in lowercase.

checking if the plant is in the calendar

if plant in planting_calendar:

The program checks if the flower’s name exists in the planting_calendar dictionary.

Retrieving the start and end dates

start_month, start_day = planting_calendar[plant]["start"]
end_month, end_day = planting_calendar[plant]["end"]

if the plant is in the calendar,the program retrieves the start and end dates of its planting season.

it gets the start date from the planting_calendar and unpacks it into two variables:

start_month and start_day.

similarly,it retrieves and unpacks the end date into end_month and end_day.

creating start_date and end_date

start_date = datetime.date(today.year, start_month, start_day)
end_date = datetime.date(today.year, end_month, end_day)

Next,the program constructs two date objects,start_date and end_date,using the current year(today.year) and the start/end months and days from the dictionary.

these start_date and end_date objects represent the first and last days of the flower’s planting season for the current year.

Handling End Dates that cross over to the Next Year

if end_date < start_date:
    end_date = datetime.date(today.year + 1, end_month, end_day)

This block checks if the end date comes before the start date in the calendar,which can happen if the planting season crosses over into the next year.

if the end_date is before the start_date the program adjusts the end date by adding one year to it.

checking if today is within the planting window

if start_date <= today <= end_date:
    print(f"It's time to plant {plant}!")

This line checks if the current date (today) is within the range of the start_date and end_date.

checking if it’s too early to plant

 

elif today < start_date:
    print(f"It's too early to plant {plant}. Start planting between {start_date.strftime('%B %d')} and {end_date.strftime('%B %d')}.")

if today’s date is before the start of the planting season,the program prints a message informing the user that it’s too early to plant.

checking if it’s too late to plant

else:
    print(f"It's too late to plant {plant} this year. The planting season was from {start_date.strftime('%B %d')} to {end_date.strftime('%B %d')}.")

if today’s date is after the end of the planting season,the program prints a message saying it’s too late to plants the flower this year.

Handling plants Not in the calendar

else:
    print(f"{plant} is not in the planting calendar.")

if the plant name is not found in the planting_calendar dictionary,the program prints a message telling the user that the flower they entered is not in the list.

This handles cases where the user might input a flower that is not in the calendar.

output

conclusion

The Planting Calendar program is a practical and user-friendly tool that helps gardeners ensure they plant flowers at the optimal time for healthy growth. By comparing the current date with the defined planting windows for each flower, the program provides clear guidance on whether it’s the right time to plant, too early, or too late.

This tool simplifies gardening decisions by automating the process of checking planting schedules, making it easy to stay on track with seasonal planting. The flexibility to add more plants or adjust planting dates makes the program adaptable to different climates and gardening needs.

Ultimately, the planting calendar helps users plan and maintain their gardens with confidence, ensuring they get the best results from their planting efforts.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top