How to add entries to an empty dictionary using a formatted key, and update the value using the same key?
Question:
I need to make an appointment calendar application in Python.
I have this empty dictionary called appointments
and I’m trying to add appointments. It’s working but for some reason it’s not accumulating the appointments:
appointments = {}
while (True):
(... I had a choice variable here that would ask the user to either make an appointment, cancel, or view list of appointments. but for right now, i am only focusing on making appointments.)
elif choice == 1: #Making an appointment
apptDate = input('Enter date (mm/dd/yy)n')
apptTime = input('Enter time (hh:mm)n')
apptDesc = input('Enter a brief description of the appointmentn')
#Checking to see if key in dictionary
if apptDate not in appointments:
newAppt = apptTime + 't' + apptDesc
appointments[apptDate] = newAppt
else:
appointments[apptDate] = newAppt
-
I have to place apptTime + 't' + apptDesc
in the appointments
dictionary using apptDate
as the key. I thought I was doing this right.
-
Check to see if apptDate
is already in the appointments
dictionary because it affects the way that it supposedly adds the new appointment.
Answers:
appointments = []
is a list
appointments = {}
is a dictionary
to check if a key is in a dictionary use
if key in dictionary:
(where key would be apptDate)
For your purposes use a defaultdict
from collections
module. This allows you to initialise your dictionary to give a key a certain value if the key is not already in there. In your case you may want to consider each date having a list of appointments, so if there are no appointments, the default dict is initialised with an empty list:
from collections import defaultdict
def default():
return []
appointments = defaultdict(default)
Then, whenever you want to add an appointment to a key, you just do
appointments['date'].append("Info")
This is pretty clean, and avoids the checking statements.
If you insist doing it your way, your last paragraph can be:
if apptDate not in appointments:
newAppt = apptTime + 't' + apptDesc
appointments[apptDate] = [newAppt]
else:
newAppt = apptTime + 't' + apptDesc
appointments[apptDate].append(newAppt)
I need to make an appointment calendar application in Python.
I have this empty dictionary called appointments
and I’m trying to add appointments. It’s working but for some reason it’s not accumulating the appointments:
appointments = {}
while (True):
(... I had a choice variable here that would ask the user to either make an appointment, cancel, or view list of appointments. but for right now, i am only focusing on making appointments.)
elif choice == 1: #Making an appointment
apptDate = input('Enter date (mm/dd/yy)n')
apptTime = input('Enter time (hh:mm)n')
apptDesc = input('Enter a brief description of the appointmentn')
#Checking to see if key in dictionary
if apptDate not in appointments:
newAppt = apptTime + 't' + apptDesc
appointments[apptDate] = newAppt
else:
appointments[apptDate] = newAppt
-
I have to place
apptTime + 't' + apptDesc
in theappointments
dictionary usingapptDate
as the key. I thought I was doing this right. -
Check to see if
apptDate
is already in theappointments
dictionary because it affects the way that it supposedly adds the new appointment.
appointments = []
is a list
appointments = {}
is a dictionary
to check if a key is in a dictionary use
if key in dictionary:
(where key would be apptDate)
For your purposes use a defaultdict
from collections
module. This allows you to initialise your dictionary to give a key a certain value if the key is not already in there. In your case you may want to consider each date having a list of appointments, so if there are no appointments, the default dict is initialised with an empty list:
from collections import defaultdict
def default():
return []
appointments = defaultdict(default)
Then, whenever you want to add an appointment to a key, you just do
appointments['date'].append("Info")
This is pretty clean, and avoids the checking statements.
If you insist doing it your way, your last paragraph can be:
if apptDate not in appointments:
newAppt = apptTime + 't' + apptDesc
appointments[apptDate] = [newAppt]
else:
newAppt = apptTime + 't' + apptDesc
appointments[apptDate].append(newAppt)