[Tutor] Adding key, value to Dictionary

David david at abbottdavid.com
Fri Mar 27 18:52:14 CET 2009


David wrote:
> 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

I also thought this would work if I did not start out with a blank 
dictionary;
def get_todo():
     key = raw_input('Enter Todo Title: ')
     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()

same result

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



More information about the Tutor mailing list