Advise of programming one of my first programs

Anatoli Hristov tolidtm at gmail.com
Sat Mar 31 07:39:32 EDT 2012


Ramit,
This seems to be more logic now "I hope" :)
#########################################
import ast
fname = 0
lname = 1
country = 2
city = 3
tel = 4
notes = 5
## Read data from file
def load_book():
 load_book = open('c:/Python27/Toli/myfile.txt', 'r')
 load_book = ast.literal_eval(load_book.read())
 return load_book
## Write data to file
def write_book(tbook):
 write_book = open('c:/Python27/Toli/myfile.txt', 'w')
 write_book.write(repr(tbook))
## Menu choice input
def get_menu_choice(text):
choice = raw_input(text)
return choice
## List data contacts
def listpb():
 ##tbook = load_book()
 print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'
 print 'Nick\t\tF.Name\t\tL.Name\t\tCountry\t\t\tCity\t\tTel'
 print '_' * 105,'\n','\t' * 13
 for val in tbook.keys():
 print val, '\t\t', tbook[val][fname], '\t', tbook[val][lname],
'\t', tbook[val][country], '\t\t', tbook[val][city], '\t\t',
tbook[val][tel],'\t\t\n'
 print '_'*105,'\n\n\n\n'
 print 'Type nickname and press <Enter> or type <Q> to exit.\n\n\n'
## Main menu
def mmenu(tbook):
 listpb()
 while True:
 text = 'Type your option: '
 choice = get_menu_choice(text)
 if choice == 'e' or choice == 'E':
 text = 'Type nickname and press <Enter> to edit: '
 choicen = get_menu_choice(text)
 if choicen in tbook:
 edit(choicen, tbook)
 elif choice == 'b' or choice == 'B':
 listpb()
 elif choice == 'd' or choice == 'D':
 text = 'Type nickname and press <Enter> for details: '
 choicen = get_menu_choice(text)
 if choicen in tbook:
 details(choicen, tbook)
 elif choice == 'q' or choice == 'Q':
 break
 else:
 print 'Selection {0} not understood.'.format(choice)
## Contact details
def details(choicen, tbook):
 sb = tbook[choicen]
 print 'Nickname: ', choicen, ' is selected\n'
 print 'First name:\t', sb[fname], '\n'
 print 'Last name:\t', sb[lname], '\n'
 print 'Country:\t', sb[country], '\n'
 print 'City:\t\t', sb[city], '\n'
 print 'Phone number:\t',sb[tel], '\n'
 print 'Memos:\n'
 print sb[notes]
 print '\n\n(E)dit\n\n'
 print '(B)ack to phonebook list\n\n'
## Edit contact
def edit(choicen, tbook):
 sb = tbook[choicen]
 fn = raw_input('New name for ' + sb[fname] + ' : ')
 if fn == '':
 pass
 else:
 sb[fname] = fn
 ln = raw_input('New name for ' + sb[lname] + ' : ')
 if ln == '':
 pass
 else:
 sb[lname] = ln
 write_book(tbook)
 details(choicen, tbook)
tbook = load_book()
mmenu(tbook)
#######################################
What you thing?
Regards
Anatoli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120331/a3260303/attachment-0001.html>


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /