Command Line Calendar syntax error

I’m not sure what’s wrong with my code. My calendar isn’t working and the terminal states that this line elif user_choice == “D”: has a syntax error.

This is my code below and this is the link to the exercise

"""The programme will create a basic calendar that allows the user to view the calendar, add an event, update or delete an existing event."""
from time import sleep, strftime
calendar = {}

def welcome():
  print "Welcome" + USER_FIRST_NAME + "."
  print "The calendar is opening."
  print "Today is: " + strftime("%A %B %d, %Y")
  print "The time is: " + strftime("%H:%M:%S")
  print "What would you like to do?"

def start_calendar():
  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 "Calendar empty"
        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 "Invalid date was entered."
      try_again = raw_input("Try Again? Y for Yes, N for No: ")
      try_again = try_again.upper()
      if try_again == "Y":
        start == False
      calendar[date] = event
      print "Event successfully added"
      print calendar
    elif user_choice == "D":
      if len(calendar.keys()) < 1:
      print "The calendar is empty."
      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 event != calendar[date]:
          print "Incorrect event was specified."
        elif user_choice == "X":
          start = False
          print "Invalid command was entered."
          start = False

Your indentations are incorrect in a number of places. Unlike some other languages which use curly braces to specify blocks of code, python relies on indentation.

Your code:

I have just edited it so that the indentation of the corresponding if, elif and else blocks match. I haven’t looked at the logic and correctness of the code. Since you wrote the code, you have a better idea of which statement belongs to which block. I am just giving you a general idea of how the indentations need to be fixed so that they are at the correct levels (you can right click on image and open in new tab for improved zoom).

Also, you wrote:

else event != calendar[date]:
          print "Incorrect event was specified."

if and elif require conditions, but you can’t specify a condition for else.

1 Like

Thank you. This is helpful.