Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Add delete event functionality #826

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 95 additions & 12 deletions projects/Calendar/displayCalendar.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import calendar

# A dictionary to store events by date
events = {}

def display_cal(year_input, month_input):
"""
Display a calendar for the desired year and month.

Parameters:
year_input (int): The year for which the calendar is to be displayed.
month_input (int): The month for which the calendar is to be displayed.

"""
import calendar

print(calendar.month(year_input, month_input))

display_events(year_input, month_input)

def fetch_year():
"""
Expand All @@ -25,7 +23,6 @@ def fetch_year():
except ValueError:
print("Invalid input. Please enter a valid year.")


def fetch_month():
"""
Function that asks the user to enter a month, validates the input, and returns the valid month.
Expand All @@ -39,8 +36,94 @@ def fetch_month():
except ValueError:
print("Invalid input. Please enter a valid month.")

def add_event(year_input, month_input):
"""
Add an event to a specific date.
"""
day_input = int(input("Enter day for the event (1-31): "))
event_title = input("Enter event title: ")

# Validate the day
if day_input < 1 or day_input > 31:
print("Invalid day. Please enter a day between 1 and 31.")
return

# Store event in dictionary
date_key = f"{year_input}-{month_input:02d}-{day_input:02d}"
events[date_key] = event_title
print(f"✅ Event '{event_title}' added to {date_key}")

def view_events():
"""
View all added events.
"""
if not events:
print("No events added yet.")
else:
print("\nUpcoming Events:")
for date, event in events.items():
print(f"{date} - {event}")

def delete_event():
"""
Deletes an event from the events dictionary.
"""
if not events:
print("No events to delete.")
return

print("\nYour Events:")
for idx, (date, event) in enumerate(events.items(), 1):
print(f"{idx}. {date} - {event}")

try:
choice = int(input("Enter the event number to delete: "))
if 1 <= choice <= len(events):
date_to_delete = list(events.keys())[choice - 1]
removed_event = events.pop(date_to_delete)
print(f"✅ Event '{removed_event}' on {date_to_delete} deleted successfully!")
else:
print("❌ Invalid event number.")
except ValueError:
print("❌ Please enter a valid number.")

def display_events(year_input, month_input):
"""
Display all events for a specific month and year.
"""
print("\nEvents in the calendar:")
for date, event in events.items():
event_year, event_month, _ = date.split("-")
if int(event_year) == year_input and int(event_month) == month_input:
print(f"{date} - {event}")

def main_menu():
year_input = fetch_year()
month_input = fetch_month()

while True:
print("\nMenu:")
print("1. Display Calendar")
print("2. Add Event")
print("3. View Events")
print("4. Delete Event")
print("5. Exit")

choice = input("Choose an option: ")

year_input = fetch_year()
month_input = fetch_month()
if choice == "1":
display_cal(year_input, month_input)
elif choice == "2":
add_event(year_input, month_input)
elif choice == "3":
view_events()
elif choice == "4":
delete_event()
elif choice == "5":
print("Exiting...")
break
else:
print("Invalid option! Please try again.")

display_cal(year_input, month_input)
if __name__ == "__main__":
main_menu()