Manage and view events on your calendar
Introduction
In this project, we will create a simple Calendar App that allows users to add and view events for specific dates. The app will be developed using Python, and it will make use of basic data structures to manage events. The goal of this program is to allow users to store, retrieve, and display events for given dates on their calendar.
Objective
- Create a calendar to store events.
- Allow users to add events for specific dates.
- Allow users to view events for a selected date.
- Implement the program in Python with a simple text-based interface.
Python Code for Calendar App
# Simple Calendar App in Python import calendar # Dictionary to store events events = {} def add_event(date, event): # Adds an event to the specified date if date in events: events[date].append(event) else: events[date] = [event] print(f"Event added on {date}: {event}") def view_events(date): # Displays events for the specified date if date in events: print(f"Events on {date}:") for event in events[date]: print(f"- {event}") else: print(f"No events found on {date}.") def show_menu(): # Displays the menu and handles user input print("\n--- Calendar App Menu ---") print("1. Add Event") print("2. View Events") print("3. Exit") def main(): # Main function to run the app while True: show_menu() choice = input("Choose an option: ") if choice == '1': date = input("Enter the date (YYYY-MM-DD): ") event = input("Enter the event description: ") add_event(date, event) elif choice == '2': date = input("Enter the date (YYYY-MM-DD) to view events: ") view_events(date) elif choice == '3': print("Exiting Calendar App.") break else: print("Invalid choice. Please select a valid option.") # Run the app if __name__ == "__main__": main()
Explanation of the Program Structure
The program is divided into multiple sections:
- add_event(date, event): This function takes a date and an event description as input and adds the event to the dictionary for that date.
- view_events(date): This function displays all events for a given date by retrieving them from the dictionary.
- show_menu(): This function displays the menu of options to the user, including options to add an event, view events, or exit the app.
- main(): This is the main function that runs the app. It displays the menu, accepts user input, and calls the appropriate function based on user choices.
How to Run the Program
To run the Calendar App, follow these steps:
- Ensure you have Python installed on your machine. You can download it from here.
- Create a new Python file (e.g.,
calendar_app.py
) and paste the provided code into the file. - Open a terminal or command prompt and navigate to the directory where the Python file is saved.
- Run the program by typing
python calendar_app.py
in the terminal or command prompt. - Follow the on-screen instructions to add or view events.