Advise of programming one of my first programs

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Mar 29 17:04:25 EDT 2012


>>From the Zen of Python, "Simple is better than complex." It is a good programming
mentality.
>Complex is better than complicated. :p

Absolutely! Too bad your version would be considered the more “complicated” version ;)

>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


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.  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120329/0810cf91/attachment-0001.html>


More information about the Python-list mailing list