Command Line Calendar How can I fix this?


#1

I did exactly what I was told in this project, both the written instruction and the video walkthrough AND the code doesn’t execute how it is supposed to!!

So my problem is when I delete an event, first I get my ELSE message, and then the message Im supposed to get after I’ve deleted an event successfully!! WHY???

Here is how that part looks in the terminal:

Enter A to Add, U to Update, V to View, D to Delete, X to Exit: d
What event? EVENT B
Incorrect event was specified!
The event was successfully deleted.

Here is that part of the code(the way they told us to write it in the instructions and in the video walkthrough:

elif user_choice == "D":
      if len(calendar.keys()) < 1:
        print "The calendar is empty."
      else:
        event = raw_input("What event? ")
        for date in calendar.keys():
          if event == calendar[date]:
            del calendar[date]
            print "The event was successfully deleted."
            print calendar
          else:
            print "Incorrect event was specified!"

Here is my full code:

from time import sleep, strftime

USER_FIRST_NAME = "Phil"

calendar = {}

def welcome():
  print "Welcome, " + USER_FIRST_NAME + "."
  print "Opening calendar..."
  sleep(1)
  print "Current date: " + strftime("%A %B %d, %Y")
  print "Current time: " + strftime("%H:%M:%S")
  sleep(1)
  print "What would you like to do?"
  
def start_calendar():
  welcome()
  start = True
  while start:
    user_choice = raw_input("Enter A to Add, U to Update, V to View, D to Delete, X to Exit: ")
    user_choice = user_choice.upper()
    if user_choice == "V":
      if len(calendar.keys()) < 1:
        print "The calendar is empty."
      else:
        print calendar
    elif user_choice == "U":
      date = raw_input("What date? ")
      update = raw_input("Enter the update: ")
      calendar[date] = update
      print "Update successful"
      print calendar
    elif user_choice == "A":
      event = raw_input("Enter event: ")
      date = raw_input("Enter date (MM/DD/YYYY): ")
      if len(date) > 10 or int(date[6:]) < int(strftime("%Y")):
        print "You entered an invalid date!"
        try_again = raw_input("Try Again? Y for Yes, N for No: ")
        try_again = try_again.upper()
        if try_again == "Y":
          continue
        else:
          start = False
      else:
        calendar[date] = event
        print "The event was successfully added!"
        print calendar
    elif user_choice == "D":
      if len(calendar.keys()) < 1:
        print "The calendar is empty."
      else:
        event = raw_input("What event? ")
        for date in calendar.keys():
          if event == calendar[date]:
            del calendar[date]
            print "The event was successfully deleted."
            print calendar
          else:
            print "Incorrect event was specified!"
    elif user_choice == "X":
      start = False
    else:
      print "Invalid command was entered!"
      start = False
      
start_calendar()

And here is everything I did in the terminal:

python Calendar.py
Welcome, Phil.
Opening calendar...
Current date: Monday August 20, 2018
Current time: 20:23:07
What would you like to do?
Enter A to Add, U to Update, V to View, D to Delete, X to Exit: a
Enter event: EVENT A
Enter date (MM/DD/YYYY): 08/22/2018
The event was successfully added!
{'08/22/2018': 'EVENT A'}
Enter A to Add, U to Update, V to View, D to Delete, X to Exit: a
Enter event: EVENT B
Enter date (MM/DD/YYYY): 08/23/2018
The event was successfully added!
{'08/22/2018': 'EVENT A', '08/23/2018': 'EVENT B'}
Enter A to Add, U to Update, V to View, D to Delete, X to Exit: d
What event? EVENT B
Incorrect event was specified!
The event was successfully deleted.
{'08/22/2018': 'EVENT A'}
Enter A to Add, U to Update, V to View, D to Delete, X to Exit: x

Any suggestions om how to re-write the code so that it does what it is supposed to do?


I think I may have found an error in the instructions for this project
#2

Hi @codenamephil ,

Consider this loop …

        for date in calendar.keys():
          if event == calendar[date]:
            del calendar[date]
            print "The event was successfully deleted."
            print calendar
          else:
            print "Incorrect event was specified!"

Assuming that the user entered a date that does exist as a key within a calendar that contains events for multiple dates …

  • What happens when the targeted date is found? Might the loop continue to execute even after the event is deleted?
  • What happens if the loop has not yet visited all the events, and it encounters a date that is not the targeted date? It is still too early to conclude that an invalid date was entered, yet the else gets executed.

Consider using a break at a strategic location and having the else apply to the loop, instead of to the if statement. An adjustment of indentation can accomplish the latter.


#3

Is this what you had in mind?

        for date in calendar.keys():
          if event == calendar[date]:
            del calendar[date]
            print "The event was successfully deleted."
            print calendar
            break
        else:
          print "Incorrect event was specified!"

I put a break right under print calendar and else under for and it worked :slight_smile:

But, isnt it a bit weird to put else there, because isnt else supposed to be a part of the that particular if condition?


#4

In that new position, the else is matched with the for loop. Accordingly, it executes only if the for loop completes all of its iterations without being interrupted by something such as a break. In the context of this project, that occurs when the chosen date does not exist as a key within the calendar, and therefore constitutes an incorrect event.

See Learn Python: For / else.


#5

Oh yes I remember that lesson now. So Codecademy made a mistake in their instruction basically…


#6

Yeah, instruction 42 is where they should have had the user insert a break in the for loop and match the else with that loop.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.