update_dict(data_holder, filename)
>                else:       # file no exist
>                    write_book(data_holder, filename)
>                break
>            else:
>                d.clear()   #clear dict
>                break


There's two elses, but no inital if:....
There's a place to start, right there.


On Wed, 19 Jan 2005 21:27:24 -0500, Jacob S. <[EMAIL PROTECTED]> wrote:
> Hi everyone, sent this on to the list as told to.
> cc to eri to verify my sending to list...
> ;-) Jacob
> 
> > dear jacob,
> >
> > sorry to send this to you but if you may, kindly send to tutor list as im
> > no longer subscribed.  my problem is in the update dict portion: it just
> > doesnt update regardless how many contacts i add. kindly advise where
> > my mistake is or code gone wrong. the rest of the options i will do on my
> > own so just hold off the helps for now. appreciate all your good help.
> >
> > please cc to this account.
> >
> > --
> > regards,
> > erimendz
> >
> >
> > #!/usr/bin/env python
> >
> > import cPickle, os, sys, re, stat
> > #import string
> >
> > ## Global variables
> >
> >
> > home = '~'
> > filename = os.path.join(os.path.expanduser(home), 'abook.dat')
> > data_holder = {}
> >
> >
> >
> > ## Functions
> > ##################################################################
> >
> >
> > def about_program():
> >            print
> >            print '\t'*2, 'A simple address book written in Python'
> >            raw_input('\nPress <Enter> to continue...')
> >
> >
> > ## add_contact ##
> > def add_contact(d):
> >    while True:
> >        name = add_name()
> >        email = add_email()
> >        d[name] = email
> >        print 'Add another contact? '
> >        ans = ask_yes_no()
> >        if ans == 0:    # no
> >            print 'Save to address book? '
> >            get_ans = ask_yes_no()
> >            if get_ans == 1:    # yes
> >                #collected = d
> >                check = if_file_exist(filename)
> >                if check is True:
> >      update_dict(data_holder, filename)
> >                else:       # file no exist
> >                    write_book(data_holder, filename)
> >                break
> >            else:
> >                d.clear()   #clear dict
> >                break
> >
> > def add_name():
> >    msg = 'Enter contact name: '
> >    while True:
> >        try:
> >            name = raw_input(msg)
> >            if len(name) != 0:
> >                if len(name) <= 20:
> >                    return name
> >                else:
> >                    print 'Name too long: please limit to 20 characters'
> >            else:
> >                print 'Try again: blank not allowed!'
> >        except EOFError:    # catch ^C-D keypress
> >            print
> >
> > def add_email():
> >    msg = 'Enter email address: '
> >    while True:
> >        try:
> >            email = raw_input(msg)
> >            if len(email) == 0:
> >                print 'Blank not allowed!'
> >            else:
> >                valid_format =
> > r'[EMAIL PROTECTED](\.[-a-z0-9]+)*\.(com$|\
> >                        edu$|net$|gov$|mil$|org$|int$|aero$|biz$|coop$|
> > museum$|pro$|info$)'
> >                valid_email = re.compile(valid_format)
> >                if valid_email.match(email):
> >                    return email
> >                else:
> >                    print '%s is not a valid address: try again!' % email
> >        except EOFError:
> >            print
> >
> >
> > def ask_yes_no():
> >    try:
> > ask = raw_input('Yes or No? [y|N] ')
> > if ask.lower() in ['y', 'ye', 'yes', 'yep', 'ok']:
> >     return 1    # yes
> > else:
> >     return 0    # no
> >    except EOFError:
> > print
> >
> >
> > def if_file_exist(f):
> >    ''' test if file exists; returns boolean '''
> >
> >    return os.path.exists(os.path.join(os.path.expanduser('~'), f))
> >
> > def get_filesize(f):
> >    ''' check file size '''
> >
> >    return os.stat(os.path.join(os.path.expanduser('~'), f))[stat.ST_SIZE]
> >
> >
> > def write_book(d, f):
> >    write = open(f, 'wb')
> >    cPickle.dump(d, write)
> >    write.close()
> >
> > def update_dict(d, f):
> >    ''' update the saved dictionary file '''
> >
> >    read = open(f, 'rb')
> >    newdic = cPickle.load(read)
> >    newdic.update(d)
> >    read.close()
> >
> >
> >
> >
> > ## view_abook() ##
> > def view_abook(d, f):
> >    check = if_file_exist(f)
> >    if check is True:
> >        # load file and pretty print
> >        read = open(f, 'rb')
> >        d = cPickle.load(read)
> >        for k, v in d.iteritems():
> >            print '%s\t%s' % (k, v)
> >        read.close()
> >    else:
> >        print 'no contacts listed!'
> >
> >
> >
> > ## function tester, sort of ##
> > def ifaccessible(f):
> >    ''' test if file is accessible by user; returns boolean '''
> >
> >    return os.access(os.path.join(os.path.expanduser('~'), f), os.F_OK)
> >
> >
> >
> > def main():
> >    while True:
> >        select = main_menu()
> >        while True:
> >            if select in [ '1', 'p']:
> >                about_program()
> >                break
> >            elif select in [ '2', 'a']:
> >                add_contact(data_holder)
> >                break
> >            elif select in [ '3', 's']:
> >                print "save_changes()"
> >                break
> >            elif select in [ '4', 'v']:
> >                view_abook(data_holder, filename)
> >                break
> >            elif select in [ '5', 'f']:
> >                print "find_contact()"
> >                break
> >            elif select in [ '6', 'e']:
> >                print "edit_contact()"
> >                break
> >            elif select in [ '7', 'r']:
> >                print "remove_contact()"
> >                break
> >            elif select in [ '0', 't', 'T']:
> >                #print if_file_exist(filename)
> >                #print get_filesize(filename)
> >                try:
> >                    print get_filesize(filename)
> >                except OSError:
> >                    print '%s not found!' % filename
> >                break
> >            elif select in [ '8', 'q']:
> >                sys.exit('Goodbye')
> >            else:
> >                print "'%s' is invalid option!" % select
> >                break
> >
> >
> >
> > def main_menu():
> >    '''Show menu options'''
> >
> >    print """
> >            MENU OPTIONS
> >            [1] About This [P]rogram
> >            [2] [A]dd Contact
> >            [3] [S]ave Changes
> >            [4] [V]iew Address Book
> >            [5] [F]ind Contact
> >            [6] [E]dit Contact
> >            [7] [R]emove Contact
> >            [0] [T]est function
> >            [8] [Q]uit
> >    """
> >
> >    while 1:
> >        try:
> >            ask = '\nSelect an option: '
> >            opt = raw_input(ask)[0]
> >            #if opt.isalpha() == True:      # redundant True
> >            if opt.isalpha():
> >                return opt.lower()
> >            return opt
> >        except (IndexError, EOFError):  # catch <Enter>, <^C-D> keys
> >            pass
> >
> >
> > ## main program portion ##
> > if __name__ == '__main__':
> >    main()
> >
> >
> >
> >
> >
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to