Advise of programming one of my first programs

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Mar 28 00:39:32 CEST 2012


> 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()

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

You are correct it is not. :) You code is overly complex making it harder
to understand. Try and reduce the problem to the least number of tasks you need.
From the Zen of Python, "Simple is better than complex." It is a good programming 
mentality. 

1. function that returns the loaded book
def load_book():
    load_book = open('c:/Python27/Toli/myfile.txt', 'r')
    load_book = eval(load_book.read())
    return load_book! 

2. A system to navigate your program.
def mmenu():
    # load tbook here
    while True:
        choicem = get_menu_choice()
        if choicem == 'e' or choicem == 'E':
              book = get_book_name()
              edit( tbook, book )
        elif choicem == 'd' or choicem == 'D':
              book = get_book_name()
              details( tbook, book )
        elif choicem =='Q' or choicem == 'q':
              break # end loop to exit program
        else:
              print 'Selection {0} not understood.'.format( choicem )I have given you more functions

3. function to write an edited book
def write_book(tbook):
    write_book = open('c:/Python27/Toli/myfile.txt', 'w')
    write_book.write(str(tbook)) 
    # I think repr would be more accurate than str here.

4. Function to print the entire book
def listpb( tbook ):
    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'

5. Get input from user
def get_menu_choice():
    choice = raw_input('input: ')
    return choice

6. A function to get book name from user
def get_book_name(tbook):
    # write this and do not use global

6. A function to print an entry from the book
def details( tbook, choice ):
    # write this, no menu interaction allowed

7. A function to edit an entry from the book
def edit( tbook, choice ):
    # write this, no menu interaction allowed 
    # you can ask the user what you need to change the values

I do not think you need any other functions. Now you just need to finsh all the functions
and put it all together.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Python-list mailing list