Hi CC community!
I am currently enjoying the l2code Python program!
At the Calendar project, instruction 42, I get told to create an else statement within a for loop after the if statement. This is incase the calendar item does not exist. The problem I encounter is the following:
if the event exists and the program deletes the event, it still executes the else statement in my for loop. My initial attempt was to use list(calendar); I figured that list may constantly update the list from calendar, making it so that when if is executed, the list does not contain the item and else is true, so else gets executed aswell. So I tried to use copy.deepcopy. Now the problem is in reverse!
note: I am using python 3 here, because my own interpreter is based on 3 - I wasnât aware of the python versions untill I was 50% down the road in the CC verse. Too late to turn back!
Output with list(calendar):
what event do you want to delete?:
H
H succesfully deleted
{â20/20/2020â: âGâ}
H does not exist
output with copy.deepcopy(calendar)
what event do you want to delete?:
H
H does not exist
H succesfully deleted
{â20/20/2020â: âGâ}
Code:
elif user_choice == 'D': ## Block for Deleting Values in caldenar
calendarcopy = copy.deepcopy(calendar)
if len(calendar.keys()) < 1:
print ("Calendar is empty")
else:
event = input("what event do you want to delete?: \n")
for date in calendarcopy:
if event == calendarcopy[date]:
del calendar[date]
print (event + " succesfully deleted")
print (calendar)
else:
print (event + " does not exist")
Any insights would be greatly appreciated!