I am new to programming in Python and I am just wondering if I could have some help.

I am trying to write a program which creates diary entries for events and I am currently trying to convert the raw_input from the user into a date (yyyy/mm/dd) and time (24hr hh:mm). I have tried looking around for a solution but nothing I do seems to work.

I am also trying to append each event to file and display them on separate lines when they are loaded from the file.

Here is my code up to now:

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()
while True:
    menu_choice = input("Select Menu Item (1-4): ")
    if menu_choice == 1:
        print "Add Event"
        date = input("Date: ")
        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
        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:
        filename = raw_input("Filename to load: ")
        pickle_file = open(filename, "r")
        events = cPickle.load(pickle_file)
        pickle_file.close()
        print "Events:", events
    elif menu_choice == 4:
        break
    else:
        print_menu()
 
print "Goodbye"

Any help would be greatly appreciated!

Recommended Answers

All 17 Replies

Your code looks like it's running fine to me. Would you be able to edit your post and put

tags around the code? It's hard to read. I also don't know what exactly you want. It's definitely taking the input how you want it. Perhaps just ask the user to put it in the format dd/mm/yy?

Your code looks like it's running fine to me. Would you be able to edit your post and put

tags around the code? It's hard to read. I also don't know what exactly you want. It's definitely taking the input how you want it. Perhaps just ask the user to put it in the format dd/mm/yy?

Sorry I forgot to add the tags.

The code I have provided does run however I now need to format the date and time so when a user enters the value as text it is converted into the format yyyy/mm/dd and hh:mm. Another problem I have at the minute is when more than 1 event is added and saved to file, when it is loaded each event is displayed all on one line and the separate events need to be on separate lines with the 6 field titles (date, time, title, description, location, attendees) displayed also. (I hope this makes sense)

Here is my code, hope it's now clearer:

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()
while True:
menu_choice = input("Select Menu Item (1-4): ")
if menu_choice == 1:
print "Add Event"
date = input("Date: ")
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
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:
filename = raw_input("Filename to load: ")
pickle_file = open(filename, "r")
events = cPickle.load(pickle_file)
pickle_file.close()
print "Events:", events
elif menu_choice == 4:
break
else:
print_menu()

print "Goodbye"

Here you go. I still don't know how the user will be inputting data, so I included two choices for you. Just get rid of the one you don't want. I was half contemplating on getting rid of whitespace to make you tab each one like you did, but I left it in to be nice =]

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()
while True:
    menu_choice = input("Select Menu Item (1-4): ")
    if menu_choice == 1:
        print "Add Event"
        date = input("Date: ")
        # yyyy/mm/dd
        # Assuming that the user inputs this: 20100313
        date = date[0:4]+"/"+date[4:6]+"/"+date[6:8]
        # Assuming that the user inputs this: 2010, 3, 13
        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)
        #
        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
        # Add a newline to seperate each entry
        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:
        filename = raw_input("Filename to load: ")
        pickle_file = open(filename, "r")
        events = cPickle.load(pickle_file)
        pickle_file.close()
        print "Events:", events
    elif menu_choice == 4:
        break
    else:
        print_menu()

print "Goodbye"

Here you go. I still don't know how the user will be inputting data, so I included two choices for you. Just get rid of the one you don't want. I was half contemplating on getting rid of whitespace to make you tab each one like you did, but I left it in to be nice =]

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()
while True:
    menu_choice = input("Select Menu Item (1-4): ")
    if menu_choice == 1:
        print "Add Event"
        date = input("Date: ")
        # yyyy/mm/dd
        # Assuming that the user inputs this: 20100313
        date = date[0:4]+"/"+date[4:6]+"/"+date[6:8]
        # Assuming that the user inputs this: 2010, 3, 13
        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)
        #
        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
        # Add a newline to seperate each entry
        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:
        filename = raw_input("Filename to load: ")
        pickle_file = open(filename, "r")
        events = cPickle.load(pickle_file)
        pickle_file.close()
        print "Events:", events
    elif menu_choice == 4:
        break
    else:
        print_menu()

print "Goodbye"

I like the method for the date input assuming the user enters 2010, 03, 13 but (I dont know whether it's me) it doesn't seem to be working. I keep getting the error:
date = date.split(', ')
AttributeError: 'tuple' object has no attribute 'split'

and for the adding of a new line to separate each entry, I have the following error:
TypeError: cannot concatenate 'str' and 'int' objects

Had to get rid of the commented part of code (First block), then change date = input() to date = raw_input(). You still have to change the way the event load presents itself. But I fixed the major part for you:

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()
while True:
    menu_choice = input("Select Menu Item (1-4): ")
    if menu_choice == 1:
        print "Add Event"
        date = raw_input("Date: ")
        # yyyy/mm/dd
        # Assuming that the user inputs this: 2010, 3, 13
        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)
        #
        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
        # Add a newline to seperate each entry
        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:
        filename = raw_input("Filename to load: ")
        pickle_file = open(filename, "r")
        events = cPickle.load(pickle_file)
        pickle_file.close()
        print "Events:", events
    elif menu_choice == 4:
        break
    else:
        print_menu()

print "Goodbye"

My output:

1. Add an Event
2. Save Events to File
3. Load Events from File
4. Quit
()
Select Menu Item (1-4): 1
Add Event
Date: 2010, 3, 13
Time: 22 13 3
Title: Here
Description: No
Location: Here
Attendee(s): It
Select Menu Item (1-4): 2
Filename to save: Here.txt
Select Menu Item (1-4): 3
Filename to load: Here.txt
Events: [('Date: 2010/03/13', 'Time: 22 13 3', 'Title: Here', 'Description: No', 'Location: Here', 'Attendee(s): It\n')]
Select Menu Item (1-4):

Thank you for that.

The only other bit I need help with is the loading of the events from the file. They are saving and loading all of the information but when they display they're all on one line with square brackets and commas separating them and I need each event to be on a separate line without the square brackets and commas between each field
It is currently displaying like this:

Events: [('Date: 2010/03/13', 'Time: 2am', 'Title: emma', 'Description: test', 'Location: home', 'Attendee(s): emma, sam\n'), ('Date: 2010/03/26', 'Time: 4pm', 'Title: ebiz', 'Description: deadline', 'Location: furnival', 'Attendee(s): emma\n')]

..the new line '\n' isn't working.

Sorry for all of the questions, I am very new to this as I said.

Thank You in advance!

# Separate the events at the '\n' marker
for x in events[0].split('\n'):
    # Iterate through the list of each entry
    for y in x:
        # print the result
        print y
# Separate the events at the '\n' marker
for x in events[0].split('\n'):
    # Iterate through the list of each entry
    for y in x:
        # print the result
        print y

I'm really sorry but where should that code be placed, I just tried it within the file load but it's not working, I get an error: AttributeError: 'tuple' object has no attribute 'split'

Okay, sorry. The tuple appears to not have a .split() function, only a list does. So, we shall go ahead and convert the tuple to a list, then iterate through the split:

n = []
for x in events:
    n.append(x)
# Make the tuple to a list
events = n[:]
# NOTE:
# You could just do a conversion using the list() function;
# IE. events = list(events)
# Less typing, but the other way you'll understand how the tuple and list works
# Separate the events at the '\n' marker
for x in events[0].split('\n'):
    # Iterate through the list of each entry
    for y in x:
        # print the result
        print y

I still cant get it to work

This is how I am adding the code (I'm not sure I'm placing it where I should):

elif menu_choice == 3:
        filename = raw_input("Filename to load: ")
        pickle_file = open(filename, "r")
        events = cPickle.load(pickle_file)
        pickle_file.close()
        n = []
        for x in events:
            n.append(x)
        events = n[:]
        for x in events[0].split('\n'):
        # Iterate through the list of each entry
                for y in x:
        # print the result
                    print y
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()
while True:
    menu_choice = input("Select Menu Item (1-4): ")
    if menu_choice == 1:
        print "Add Event"
        date = raw_input("Date: ")
        # yyyy/mm/dd
        # Assuming that the user inputs this: 2010, 3, 13
        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)
        #
        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
        # Add a newline to seperate each entry
        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:
        filename = raw_input("Filename to load: ")
        pickle_file = open(filename, "r")
        events = cPickle.load(pickle_file)
        pickle_file.close()
        events = list(events[0])
        print "Events:"
        for x in events:
            print "       "+x
    elif menu_choice == 4:
        break
    else:
        print_menu()

print "Goodbye"

My Output:

1. Add an Event
2. Save Events to File
3. Load Events from File
4. Quit
()
Select Menu Item (1-4): 1
Add Event
Date: 2010, 3, 13
Time: 3:49
Title: Event 1
Description: None
Location: Here
Attendee(s): Me
Select Menu Item (1-4): 2
Filename to save: This.txt
Select Menu Item (1-4): 3
Filename to load: This.txt
Events:
       Date: 2010/03/13
       Time: 3:49
       Title: Event 1
       Description: None
       Location: Here
       Attendee(s): Me

One last thing. Instead of each field, date, time etc. being on separate lines, all 6 of them need to be on the same line separated by commas for each event in the file,
for e.g. Date: 2010/03/13, Time: 21:00, Title: Event 1 ..etc.
Date: 2010/03/14, Time: 10:00, Title: Event 2 ..etc.

So for each event all of the information should be on one line with a tab between each field. Could you maybe show me how this can be done?

Thank You very much!

elif menu_choice == 3:
        filename = raw_input("Filename to load: ")
        pickle_file = open(filename, "r")
        events = cPickle.load(pickle_file)
        pickle_file.close()
        events = list(events[0])
        tabs = ""
        for x in events[:-1]:
            tabs += x + "\t"
        tabs += events[-1]
        print "Events:" + tabs

That works fine thank you, however, when I enter more than one event it saves it to the file correctly (both events are saved) but when it is loaded and printed it only displays the first event in the file.

Why could this be?

Thanks again

Change events = list(events[0] to events = list(events).split('\n'). Then make a loop to iterate through each events listing, then put the original loop inside of that. Please try and figure this one out before asking for help. You will have to show me all the tries you have done before expecting me to help with your homework anymore. No offense, but you should be trying to figure it out on your own. That should be a requirement of being a programmer: able to figure out why something works on your own by modifying what you are working on

Ive managed to work it out. Thank You for all your help and sorry for expecting too much.

You're welcome, and it's quite all right. Just a lot of code writing xP. Sorry if it seems like I came off as a butthead, didn't mean to

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.