Python Calendar.py

Hi all,

I been working on the calendar project in the python course and have stumbled across the syntax error. I’ve tried to look at the support video, gone word for word with the hints and the video and am still getting this issue. Could someone look at it and maybe support with the issue please?

my code

from time import sleep, strftime

USER_FIRST_NAME = "Dave"
calendar = {}

def welcome():
  print "welcome, " + USER_FIRST_NAME + "."
  print "Calendar is opening..."
  sleep(1)
  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():
  welcome()
  start = True
  while start:
    user_choice = raw_input("Please 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."
      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_input == "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"
        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 "Your event was successfully added"
        print calendar
    elif user_choice == "D":
      if len(calendar.keys()) < 1:
        print "Your 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"
          else:
            print "incorrect event was specified"
      elif user_choice == "X":
        start = False
      else:
        print "Invalid command was entered!"
        start = False
start_calendar()

error

$ python Calendar.py
  File "Calendar.py", line 37
    if len(date) > 10 or int(date[6:]) < int(strftime("%Y")):
                                                                                     ^
SyntaxError: invalid syntax
$ 

thanks in advance for your support

Syntax errors can occur for a lot of reasons but one of the trickiest ones to spot can be unmatched parentheses as they don’t always flag an error on the expected line. If you’ve checked the validity of the expressions on that line separately then look for any unmatched (, { , [ and such on any preceding statements.

2 Likes

Thanks for your reply. Though I appreciate your response, I’m struggling to find where the problem is. The actually error has the ^ icon under the code line pointing at the colon. (the ^ is actually on another line below the colon) normally the ^ is right under the error?

   : 

(extra line)
^

thanks

sorry that example did not make sense!

    :
#extra line
    ^

Try taking away the ( infront of len(date). It seems like it doesn’t match any other ).

yeah, tried all the different parentheses combinations. It just won’t run the program. It frustrating because there is no solution to check it to for errors

Hey so I just ran a debug of your program, look here:

You also didn’t close the parenthesis.
Of course, that isn’t the only error.

OMG I was so focused on looking for the issue in front of this line, I didn’t bother to check behind it!! I even rewrote the date line just to double check. It’s started and I’m now trying to debug my errors when inputing. Thanks!

1 Like

No problem! It’s things like this that making having a code editor really powerful. I suggest downloading vscode (any will work, but I like vscode).
By the way, your code gives me possible class vibes.

class Calender:
  def __init__(self):
    self.calender = {}
  def add():
    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"
        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 "Your event was successfully added"
        print calendar

Oooh the possibilities!! But anyways, I’m happy your code is getting back on track!

3 Likes

Thanks for your support guys. I found the other errors and now the calendar is working and I’m on to the next module…hurrah!!

1 Like

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