So how do you create a module

Remember: a module is just a file containing some Python code. So, take the code that you want to share out of the gym_pos . py file:

def save_transaction(price, credit_card, description): file = open("transactions.txt", "a")

Then save this code in a file called transactions .py. You have just created a new module.

Once you've created the module, you then need to tell the programs to use it. When you were using library code you needed to import it. You do the same thing with your own modules. So, instead of using library code from the Standard Python Library, you're really just using library code that you've written yourself. You can add this line to the top of each of your programs:

TV* means V» "the tode m This line needs to be added named module-

from transactions import * This means W _ everything inside transadt.ons.py the module as if it is code within

your program.

With this line, you are telling Python that you want to run the code in the transactions . py file and this allows you to access whatever code the module contains as if it is just part of your program.

It's time to fix the programs.

tale of two programs ien your pencil ien your pencil

These are the two POS programs. Here is the code to the one used in the coffee bar (that you wrote):

This is -the eode "to "the "dofCeejpos.p/" program.

def save transaction(price, credit card, description): file = open("transactions.txt", "a")

file.write("%16s%07d%16s\n" % (credit card, price * 100, description)) file.close()

items = ["DONUT", "LATTE", "FILTER", "MUFFIN"] prices = [1.50, 2.20, 1.80, 1.20] running = True while running: option = 1 for choice in items:

print(str(option) + ". " + choice) option = option + 1 print(str(option) + ". Quit") choice = int(input("Choose an option: ")) if choice == option: running = False else:

credit card = input("Credit card number: ")

save transaction(prices[choice - 1], credit card, items[choice - 1])

The other program is very similar (which your friend created def save transaction(price, credit card, file = open("transactions.txt", "a") file.write("%07d%16s%16s\n" % (price file.close()

for use in the gym):

description):

* 100, credit card, description))

items = ["WORKOUT", "WEIGHTS", "BIKES"] prices = [35.0, 10.0, 8.0] running = True while running: option = 1 for choice in items :

print(str(option) + ". " + choice) option = option + 1 print(str(option) + ". Quit") choice = int(input("Choose an option: ")) if choice == option: running = False else:

credit card = input("Credit card number: ")

save transaction(prices[choice - 1], credit card, items[choice - 1])

Using a pencil, modify the two programs so that they use the transactions .py module. Then write what you think should go into the transactions . py module here:

transactions module

These are the two POS programs. Here is the code to the one used in the coffee bar (that you wrote):

v_de-£ save—transaction. (price,—ciedi t—card, description) :— file - open("transactions.txt", "a")

— fi lo-w i ilo("%lhs"%07d%l 6s\ n " % ("credit_card,—price—*—100, description))

file.close() from transactions import *

items - ["DONUT", "LATTE", "FILTER", "MUFFIN"] prices - [1.50, 2.20, 1.80, 1.20] running - True while running: option - 1 for choice in items:

print(str(option) + ". " + choice) option - option + 1 print(str(option) + ". Quit") choice - int(input("Choose an option: ")) if choice -- option: running - False else:

credit card - input("Credit card number: ")

save transaction(prices[choice - 1], credit card, items[choice - 1])

The other program is very similar (which your friend created for use in the gym):

—credi t_card,—description) : file = open("transaclionb.txt",—"a") [~ file.Wiile("%07d%1Cb%1Cs\ii" %—(price * 100, credit_card,—description) ) file.close() from transactions import *

items = ["WORKOUT", "WEIGHTS", "BIKES"] prices = [35.0, 10.0, 8.0] running = True while running: option = 1 for choice in items:

print(str(option) + ". " + choice) option = option + 1 print(str(option) + ". Quit") choice = int(input("Choose an option: ")) if choice == option: running = False else:

credit card = input("Credit card number: ")

save transaction(prices[choice - 1], credit card, items[choice - 1])

Using a pencil, you were asked modify the two programs so that they use the transactions . py module. You were then asked to write what you think should go into the transactions .py module here:

def save_transaction(price, credit_card, description):

file = open("transactions.txt", "a")

file.write(tt%07d%|6s%|6s\n" % (price * 100, credit_card, description))

fileclose()

Make sure yoi displays the PRICE first.

Was this article helpful?

0 0

Post a comment