Wednesday 15 July 2015

python - Need to add appointments into empty dictionary -



python - Need to add appointments into empty dictionary -

i need create appointment calendar application in python , im struggling with first part.

i have empty dictionary called appointments , im trying add together appointments. working reason not accumulating appointments:

appointments = {} while (true): (... had selection variable here inquire user either create appointment, cancel, or view list of appointments. right now, focusing on making appointments.) elif selection == 1: #making appointment apptdate = input('enter date (mm/dd/yy)\n') appttime = input('enter time (hh:mm)\n') apptdesc = input('enter brief description of appointment\n') #checking see if key in dictionary if apptdate not in appointments: newappt = appttime + '\t' + apptdesc appointments[apptdate] = newappt else: appointments[apptdate] = newappt

i have place appttime + '\t' + apptdesc in appointments dictionary using apptdate key. thought doing right.

check see if apptdate in appointments dictionary because affects way supposedly add together new appointment.

any help great, give thanks you

for purposes utilize default dict (imported collections). allows initialise dictionary give key value if key not in there. in case may want consider each date having list of appointments (so if there no appointments, default dict initialised empty list:

from collections import defaultdict def default(): homecoming [] appointments = defaultdict(default)

then, whenever want add together appointment key,

appointments['date'].append("info")

this pretty clean, , avoids checking statements

edit: if insist doing way, lastly paragraph can be:

if apptdate not in appointments: newappt = appttime + '\t' + apptdesc appointments[apptdate] = [newappt] else: newappt = appttime + '\t' + apptdesc appointments[apptdate].append(newappt)

python loops dictionary while-loop choice

No comments:

Post a Comment