Advise of programming one of my first programs
Anatoli Hristov
tolidtm at gmail.com
Fri Mar 30 17:58:09 EDT 2012
>
> **
>
> Absolutely! Too bad your version would be considered the more
> “complicated” version ;)
>
I`m sure about that, but I`am also sure that every beginner passed true
that way.
> ****
>
> ** **
>
> >With the main navigation menu I will only have the option to select a
> nickname and when a nickname is selected then it loads Details of the
> contact and from loaded details I can choice Edit or back to main screen,
> like I did it the first time, or else I can do it => when 'e' pressed to
> ask for a nickname and then edit it.
>
**
>
> I was trying to simplify it to “guide” you to a more correct solution
> without feeding you the answer. Maybe I should have given you the
> explanation first to explain why you should be doing it a different way.**
> **
>
> ** **
>
> Going back to your original program (and your modifications to it), the
> original menu can cause crashing in more complicated programs and thus is
> considered a bad style. It was basically using recursion (I will touch on
> this later) but without any of the benefits. It was endlessly branching
> instead of a simple loop. Sort of like the following Menu/submenu example.
> ****
>
> ** **
>
> Menu****
>
> submenu****
>
> Menu****
>
> submenu****
>
> Menu****
>
> __ad infinitum__****
>
> ** **
>
> How does this matter? Let’s look at some simpler code below.****
>
> ** **
>
> print ‘start’****
>
> function_a() # a function is called****
>
> print ‘a’ # code inside the function****
>
> print ‘b’ # code inside the function****
>
> a = ‘ something ‘****
>
> print a****
>
> function_b() # another function call****
>
> print ‘c’ # code inside a different function****
>
> print ‘d’ # code inside a different function****
>
> print ‘end’****
>
> ** **
>
> Let us pretend we are the computer who executes one line at a time and so
> basically goes through a list of commands. The list we are going to execute
> is the following:****
>
> ** **
>
> print ‘start’****
>
> function_a()****
>
> print ‘a’****
>
> print ‘b’****
>
> a = ‘ something ‘****
>
> print a****
>
> function_b()****
>
> print ‘c’****
>
> print ‘d’****
>
> print ‘end’****
>
> ** **
>
> How does the computer know to execute “a = ‘ something ‘” after “print
> ‘b’”? It does it by storing the location where it was before it proceeds to
> the function call. That way when the end of the function is reached it
> returns to the previous spot. In essence:****
>
> ** **
>
> print ‘start’****
>
> function_a()****
>
> __store this location so I can come back__****
>
> print ‘a’****
>
> print ‘b’****
>
> __return to previous location__****
>
> a = ‘ something ‘****
>
> print a****
>
> function_b()****
>
> __store this location so I can come back__****
>
> print ‘c’****
>
> print ‘b’****
>
> __return to previous location__****
>
> print ‘end’****
>
> ** **
>
> Now what happens if “function_a” calls “function_a”? By the way, the term
> for this type of call is recursion.****
>
> ** **
>
> print ‘start’****
>
> function_a()****
>
> __store this location so I can come back__****
>
> print ‘a’****
>
> print ‘b’****
>
> function_a()****
>
> __store this location so I can come back__****
>
> print ‘a’****
>
> print ‘b’****
>
> function_a()****
>
> __store this location so I can come back__****
>
> print ‘a’****
>
> print ‘b’****
>
> function_a()****
>
> __store this location so I can come back__****
>
> print ‘a’****
>
> print ‘b’****
>
> function_a()****
>
> __store this location so I can come back__****
>
> print ‘a’****
>
> print ‘b’****
>
> function_a()****
>
> __store this location so I can come back__****
>
> **until the program ends******
>
> ** **
>
> Now each __store__ action takes up memory and when the computer (or your
> program) runs out of memory your computer crashes. Your application is
> trivial and more likely to be ended by the user instead of going through
> the tens of thousands if not hundreds of thousands that Python will let you
> take, but it is a bad practice and a habit to avoid. A real world program
> would use more memory and quit even faster than yours. Recursion has its
> place in programming, but not in this case! What you need is a simple loop.
> That is why I provided you with the menu I did. ****
>
> ** **
>
> The following menu sounds like what you want; there were a couple
> different ways I could have done this. In this version, if you type
> anything when asked for a menu choice that is not ‘e’ or ‘q’, the program
> will automatically ask you for the next book choice.****
>
> ** **
>
> def mmenu():****
>
> # load tbook here****
>
> while True:****
>
> book = get_book_choice()****
>
> details( tbook, book )****
>
> choicem = get_menu_choice()****
>
> if choicem == 'e' or choicem == 'E':****
>
> edit( tbook, book )****
>
> # save tbook here****
>
> elif choicem =='Q' or choicem == 'q':****
>
> break # end loop to exit program****
>
> ** I`m focusing on what you say and will try to follow your instructions
> for which - Thank you I really appreciate it...
>
Just before I read you mail I`ve finished my book ver. 12 with this code,
ver 13 will have all the modifications and all remarks I have from you:
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():
choice = raw_input('input: ')
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'
mmenu(tbook)
## Main menu
def mmenu(tbook):
while True:
choice = get_menu_choice()
if choice in tbook:
details(choice,tbook)
elif choice == 'q' or choice == 'Q':
break
else:
print 'Selection {0} not understood.'.format(choice)
## Details menu
def dmenu(choice, tbook):
while True:
choicem = get_menu_choice()
if choicem == 'e' or choicem == 'E':
edit(choice, tbook)
elif choicem == 'b' or choicem == 'B':
listpb()
elif choicem =='Q' or choicem == 'q':
break # end loop to exit program
else:
print 'Selection {0} not understood.'.format( choicem )
## Contact details
def details(choice, tbook):
sb = tbook[choice]
print 'Nickname: ', choice, ' 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'
dmenu(choice, tbook)
## Edit contact
def edit(choice, tbook):
sb = tbook[choice]
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(choice, tbook)
listpb()
> **
>
> ** **
>
> 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.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120330/1fd17e4c/attachment-0001.html>
More information about the Python-list
mailing list