Page 232 -
P. 232
modular programming
The other program is very similar (which your friend created for use in the gym):
def save_transaction(price, credit_card, description):
file = open("transactions.txt", "a")
file.write("%07d%16s%16s\n" % (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(“%07d%16s%16s\n" % (price * 100, credit_card, description))
file.close()
Make sure you use the code that
displays the PRICE first.
you are here 4 197