Hi Asrarahmed, There's a few questions in there, and unfortunately I don't have a copy of your book. But I'll try to answer them, and I'll change the order so that they build on each other.
>I am not sure of using time function in python.. Depending on what you're trying to do, the simplest way is to just use the datetime module. http://docs.python.org/lib/module-datetime.html >>> import datetime >>> the_time_right_now = datetime.datetime.now() This creates a datetime object. You can do all sorts of useful stuff with them, but for now, you probably just want to display the time. You can just >>> print the_time_right_now 2006-10-13 19:26:45.671000 But that may not be the most useful format. Datetime objects have a method called strftime(), and you can give it format strings as per the time module - http://docs.python.org/lib/module-time.html to change the output. For instance. >>> print the_time_right_now 2006-10-13 19:26:45.671000 >>> print the_time_now.strftime("%I:%M%p, %A %d %B %Y") 07:26PM, Friday 13 October 2006 In that case in the format string %I means hours in 1-12 format, %M is minutes, %p is AM/PM, %A is the full name of the day, %d is the day number, %B is the full month name and %Y is the full year.' Have a play with it. > I am trying to modify the userManagement program given in Core Python > Programming. It uses a dictionary to store user-password information. > The usernames are the keys and the passwords are the values. > Now I want is to add a third element; last login time. Okay. So what could do here is instead of having the password as a value, have a list or a dictionary as the value and store the password and login time in that. For example: >>> userInfo = {} >>> userInfo['Liam'] = { 'password' : 'snooze', 'last_login_time': datetime.datetime.now()} If you print userInfo, you'll see that it's saved a datetime object in there. >>> print userInfo {'Liam': {'password': 'snooze', 'last_login_time': datetime.datetime(2006, 10, 13, 19, 34, 58, 437000)}} You could then retrieve my password by >>> print userInfo['Liam']['password'] snooze Or the datetime object you created with >>> print userInfo['Liam']['last_login_time'] 2006-10-13 19:34:58.437000 > I want to store this information in a file so that the data is not > lost once the program stops execution. You'd need to use the pickle module. (Or cPickle, which is marginally faster. They work the same, more or less) >>> import pickle Pickle will turn objects into a stream of bytes, which you can save to a file. You can then reopen the file, and reload the objects. So, once you've imported pickle, you need to open a file to write to. Make sure you open it in binary mode (So file mode will be "wb") >>> save_file = open("userInfo.pck", "wb") You then use the pickle.dump() method to serialize the object - >>> pickle.dump(userInfo, save_file) Make sure you close the file afterwards. >>> save_file.close() Now, next time you start up - >>> import pickle You use the pickle.load() method to get your object back. First thing is to open the file you saved it to, (open it in binary mode). >>> load_file = open("userInfo.pck", "rb") >>> userInfo = pickle.load(load_file) >>> load_file.close() >>> print userInfo {'Liam': {'password': 'snooze', 'last_login_time': datetime.datetime(2006, 10, 13, 19, 34, 58, 437000)}} Hope that helps. Regards, Liam Clarke _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor