Page 234 -
P. 234

modular programming


          The transaction file is working great, too


           When you open up the transactions.txt file, you see this
           inside:                                                                The descriptions follow. (Note
                                              The next 16 characters are          the extra padding due to the
                   The first 7 characters     the credit card number.             “%16s” format specifier.)
                   are the price.


                                 00035006432425412474321          WORKOUT

                                 00001507649463856424326            DONUT




           Both of the records, created by each of the POS programs, are now
           correctly formatted. That’s because both programs are sharing the
           same piece of code to save the transactions.



 coffee_pos.py
                                      Phew! I just heard from the
                                      bank that transactions from
                                      both of the POS systems went
                                      through smoothly. That‛s a big
                                      relief. Good job fixing it!




                                 Looks like you saved the day.







            Q:   So modules are sort of like   Q:  So when I use import, it’s as if I   Q:  Do I have to use modules?

            containers for functions, right?  typed the code in the module directly
                                            into my program?                A: No, but the benefit of putting
            A: It’s true that most modules are used                         shareable code into a module starts to
            to store a collection of related functions.  A: Yes, that’s a good way to think about   pay off the second you use that module
            However, it is perfectly acceptable to   it. Using a shared module saves you from   in another program. Sharing code with
            put any code in a module, which is then   having to type (or cut’n’paste) all that code   modules is good programming practice.
            executed whenever the module is imported   yourself. Just import it and it’s there.
            into your program.


                                                                                      you are here 4    199
   229   230   231   232   233   234   235   236   237   238   239