Advise of programming one of my first programs

Anatoli Hristov tolidtm at gmail.com
Tue Mar 27 17:49:33 EDT 2012


>
> This was difficult, now I feel more confused it works, but I`m sure its
> not the way you wanted :)



> The use of eval is dangerous if you are not *completely* sure what is
>> being passed in. Try using pickle instead:
>> http://docs.python.org/release/2.5.2/lib/pickle-example.html
>
>  I`m sure about that I will use pickle nex time


> Maybe I can help you more by giving you a somewhere to build from. Try
> building the rest of the program given the new version of listpb below.

That was hard :)

You will need to read the chapter on Function Arguments (chapter 18?).
>
My book is translated in Bulgarian and it is chapter 13 should be the same
in the original too.

Here is the code now:

def load_book():

    load_book = open('c:/Python27/Toli/myfile.txt', 'r')
    load_book = eval(load_book.read())
    return load_book
def write_book(tbook):
    write_book = open('c:/Python27/Toli/myfile.txt', 'w')
    write_book.write(str(tbook))

def details(choice):
        sb = tbook[choice]
        print 'Nickname: ', choice, ' is selected\n'
        print 'First name:\t', sb[0], '\n'
        print 'Last name:\t', sb[1], '\n'
        print 'Country:\t', sb[2], '\n'
        print 'City:\t\t', sb[3], '\n'
        print 'Phone number:\t',sb[4], '\n'
        print 'Memos:\n'
        print sb[5]
        print '\n\n(E)dit\n\n'
        print '(B)ack to phonebook list\n\n'
        dmenu(choice)

def edit(choice):
    sb = tbook[choice]
    fn = raw_input('New name for ' + sb[0] + ' : ')
    sb[0] = fn
    ln = raw_input('New name for ' + sb[1] + ' : ')
    sb[1] = ln
    write_book(tbook)
##    filewrite = open('myfile.txt','w')
##    filewrite.write(str(tbook))
##    filewrite.close()
##    raw_input('\n\n\nPress <Enter> to return')
    details(choice)

def get_menu_choice():
choice = raw_input('input: ')
return choice



def listpb():
    global tbook
    tbook = load_book()
    print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'
    print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel'
    print '_' * 105,'\n','\t' * 13
    for val in tbook.keys():
            print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t',
tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n'
    print '_'*105,'\n\n'
    mmenu()

def mmenu():
    while True:
        choice = get_menu_choice()
        if choice in tbook:
            details(choice)
        elif choice not in tbook:
            print choice + 'Not in the book.'
            mmenu()
        elif choice =='Q' or choice =='q':
            break
        else:
            print 'Selection {0} not understood.'.format(choice) ## This is
something that I don`t understand yet


def dmenu(choice):
    while True:
        choicem = get_menu_choice()
        if choicem == 'e' or choicem == 'E':
              edit(choice)
        elif choicem == 'd' or choicem == 'D':
              book = get_book_to_edit()
              details( tbook, book )
        elif choicem =='Q' or choicem == 'q':
              break # end loop to exit program
        else:
              print 'Selection {0} not understood.'.format( choicem )

listpb()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120327/1bba4349/attachment.html>


More information about the Python-list mailing list