[Tutor] Adding key, value to Dictionary

greg whittier greg at thewhittiers.com
Fri Mar 27 18:50:10 CET 2009


On Fri, Mar 27, 2009 at 1:31 PM, David <david at abbottdavid.com> wrote:
> But I can not get this to update after the first time it is ran.
>
> def get_todo():
>    todo = {}

This set todo to an empty dictionary each time you execute get_todo.

>    key = raw_input('Enter Todo Title: ')
>    todo[key] = key
>    print '\n', key, 'has been added.'
>    print 'Next, enter date for Todo: '
>    curr_date = time.strftime('%Y %m %d', time.gmtime())
>    print 'Format as ', curr_date
>    yr = int(raw_input('\nEnter Year: '))
>    mt = int(raw_input('Enter Month: '))
>    dy = int(raw_input('Enter Day: '))
>    hr = int(raw_input('Enter Hour (24h): '))
>    mn = int(raw_input('Enter Minute (01-59): '))
>    value = [yr, mt, dy, hr, mn]
>    todo = {key:value}
>    todo[key] = value

todo = {key:value} again resets the value of todo.  You only need
todo[key]=value.

>    print todo
>    response = raw_input('Do you want to add another Todo? (y/n) ')
>    if response == 'y':
>        get_todo()
>    else:
>        print 'Goodbye'
>
> get_todo()


More information about the Tutor mailing list