Command Line Calendar BUG? HELP!


#1

I did exactly what I was told in this project and still the code doesn’t execute how it is supposed to!!

  1. When I delete an event I first I get my ELSE message, and then the message Im supposed to get after I’ve deleted an event successfully!! WHY???

2.When I choose to exit by typing x to exit the calendar, nothing happens, I just get asked again what I want to do again!! WHY???

Is it a bug?

Here is my 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 True:
    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()

Here is what 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
Enter A to Add, U to Update, V to View, D to Delete, X to Exit:

#2
  1. When I delete an event I first I get my ELSE message, and then the message Im supposed to get after I’ve deleted an event successfully!! WHY???
 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!"

In this block you iterate over every event. For every event that WASN’T the one you wanted deleted, the else block is reach. So basically it sees event A is not event B and goes to the else block. Then it sees event B is event B and deletes it.
2.When I choose to exit by typing x to exit the calendar, nothing happens, I just get asked again what I want to do again!! WHY???

elif user_choice == "X":
      start = False
    else:
      print "Invalid command was entered!"
      start = False

Making start = false does nothing.
Your loop started with while True: So it will always run. Your start variable is unused.
If you did While Start: it would work


#3

So i changed it back from While True: to While start: and it worked :smiley: but the reason I changed it from start to True to begin with is because the guy in the walkthrough did it this way…but anyways, now I know not to listen to the instructor in the video from now on hehe

Any suggestions on how I can fix it? Because both the instructions AND MATT THE VIDEO INSTRUCTOR lol did it this way


#4

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