I am creating a program which takes in fields for events, saves them to file and then loads & displays them on separate lines.
The 'date' field in my program should be in the format yyyy/mm/dd when it is entered. I have managed to implement this however my program still allows 1 number to be entered for the year when it should only allow 4 numbers for the year, 2 for the month and 2 for the day.
Please could someone show me how I could implement this in my program or how to improve the code I have, maybe by just entering the numbers e.g 20100315 rather than using the split with the commas.
import cPickle
def print_menu():
print '1. Add an Event'
print '2. Save Events to File'
print '3. Load Events from File'
print '4. Quit'
print()
event_list = []
menu_choice = 0
print_menu()
start = 0
while True:
menu_choice = input("Select Menu Item (1-4): ")
if menu_choice == 1:
try:
print "Add Event"
date = raw_input("Enter Date (yyyy,mm,dd): ")
# Assuming that the user inputs this: yyyy,mm,dd
date = date.split(',')
if len(date[0]) == 2:
date[0] = '20'+date[0]
if len(date[1]) == 1:
date[1] = '0'+date[1]
if len(date[2]) == 1:
date[2] = '0'+date[2]
date = '/'.join(date)
# if the format yyyy,mm,dd is not entered
except IndexError:
print "That was not in the format yyyy, mm, dd"
start # restarts the loop
continue
time = raw_input("Time: ")
title = raw_input("Title: ")
desc = raw_input("Description: ")
loc = raw_input("Location: ")
attend = raw_input("Attendee(s): ")
ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend + '\n'
event_list.append(ui)
elif menu_choice == 2:
filename = raw_input("Filename to save: ")
pickle_file = open(filename, "w")
cPickle.dump(event_list, pickle_file)
pickle_file.close()
elif menu_choice == 3:
try:
filename = raw_input("Filename to load: ")
pickle_file = open(filename, "r")
events = cPickle.load(pickle_file) # events is a list of events
pickle_file.close()
for event in events: # print each member of events
print "Events: %s" % '\t'.join(list(event))
except IOError:
print "No such file to load"
start
continue
elif menu_choice == 4:
break
else:
print_menu()
print "Goodbye"
This being the section containing the date:
if menu_choice == 1:
try:
print "Add Event"
date = raw_input("Enter Date (yyyy,mm,dd): ")
# Assuming that the user inputs this: yyyy,mm,dd
date = date.split(',')
if len(date[0]) == 2:
date[0] = '20'+date[0]
if len(date[1]) == 1:
date[1] = '0'+date[1]
if len(date[2]) == 1:
date[2] = '0'+date[2]
date = '/'.join(date)
# if the format yyyy,mm,dd is not entered
except IndexError:
print "That was not in the format yyyy, mm, dd"
start # restarts the loop
continue
time = raw_input("Time: ")
title = raw_input("Title: ")
desc = raw_input("Description: ")
loc = raw_input("Location: ")
attend = raw_input("Attendee(s): ")
ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend + '\n'
event_list.append(ui)
Any help would be greatly appreciated. Thank You