[Tutor] Adding key, value to Dictionary

David david at abbottdavid.com
Fri Mar 27 18:31:05 CET 2009


I am trying to make a simple Todo program and I can not get the 
dictionary to update.
This works;

#!/usr/bin/python
key = 'Clean house'
value = (1,2,3,4)
todo = {key:value}
value = (5,6,7,8)
todo['Walk Dog'] = value
print todo

results
{'Walk Dog': (5, 6, 7, 8), 'Clean house': (1, 2, 3, 4)}
OK good

But I can not get this to update after the first time it is ran.

def get_todo():
     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
     print todo
     response = raw_input('Do you want to add another Todo? (y/n) ')
     if response == 'y':
         get_todo()
     else:
         print 'Goodbye'

get_todo()

results

Enter Todo Title: Clean House

Clean House has been added.
Next, enter date for Todo:
Format as  2009 03 27

Enter Year: 2009
Enter Month: 3
Enter Day: 27
Enter Hour (24h): 13
Enter Minute (01-59): 28
{'Clean House': [2009, 3, 27, 13, 28]}
Do you want to add another Todo? (y/n) y
Enter Todo Title: Walk Dog

Walk Dog has been added.
Next, enter date for Todo:
Format as  2009 03 27

Enter Year: 2009
Enter Month: 3
Enter Day: 27
Enter Hour (24h): 14
Enter Minute (01-59): 35
{'Walk Dog': [2009, 3, 27, 14, 35]}
Do you want to add another Todo? (y/n)

Not so good:(



-- 
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu



More information about the Tutor mailing list