Calendar python Valid Date Not Working

I have a problem with my add function in my code. Here is my code of this part.

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:9]) < 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 "The event was successfully added."
    print calendar

It seemed alright to me until I test it. When I enter the actual invalid date, the function work just fine, but after I enter the date that should be a valid one, the program still throws me an invalid message. I don’t know what I did wrong or how should I fix it.

Please post the full code so we can actually run the program and test the behavior.

1 Like

Thank you for the fast response. I actually found my mistake. It was

  int(date[6:9])

It should be

 int(date[6:]) 

or

 int(date[6:10])

because the last index of date’s length in this format is excluded. I was so focus on the index start counting from 0 and I overlooked the format function that it exclude the last number I wrote. In this case, the number 9 was excluded when the condition checked my input, therefore everything I enter will be invalid.

1 Like

calender[date]= update

question, why does it work to update calendar as a list when calendar is a dictionary.

in other words………calendar {} can be updated by using calendar, why?

That is a valid way to set the value on the date key of the calendar dictionary. Perhaps a quick review of lists and dictionaries is in order?

>>> calendar = {
    '2020-08-29': None
}
>>> date = '2020-08-29'
>>> calendar[date] = 'today'
>>> print (calendar)
{'2020-08-29': 'today'}
>>> 
1 Like