So I took the instructors example. His is to print a report for Tree, Quantity, Unity Cost, Total cost. I need to make a program that has book name, total quantity sold, total sales. I changed the in file names in his example to what my file names are and changed the headings. I'm doing so horrible, I need to get this assignment in fast before I take the final :'( . Something is wrong in my calculations because the output of the program is:
Total Quantity Sold 117 Total Sales $ 8991.00
Here is my program with the notes from the instructor, I've attached the paramin and infile:
import string
def lnamelist():
#
#load name and price list function
#
#This function reads the parameter file and loads the name list and
#price list. It also keeps track of the number of elements that each
#list contains.
# Variable Type Purpose
# names string list holds tree names
# nprlst int list holds the tree price
# cnt int counts the number of tree types
# infile string file name
# line string holds file contents
# nm string holds individual tree names
# pr string holds the tree price as a string
# npr int holds the numeric tree price
#
names =[]
nprlst=[]
cnt=0
infile = open("paramin.txt",'r')
line = infile.readline()
while line != "":
nm,pr = string.split(line, ",")
npr = eval(pr)
names.append(nm)
nprlst.append(npr)
cnt = cnt + 1
line = infile.readline()
infile.close
return cnt, nprlst, names
def laccums (cnt,nprlst):
#
#load accumulator lists function.
#
#This function sets up the accumulator lists, reads the input
#file, calculates the total sales for that record and adds to
#the appropriate accumulator.
#
# Variable Type Purpose
# qtyarray int list quantity accumulator list
# tprarray int list price accumulator list
# infile string file name
# type int holds numeric code for tree type
# qty int holds quantity sold for the individual record
# tp int holds total price for the individual record
#
qtyarray = []
tprarray = []
infile = open("infile.txt",'r')
# This makes sure that the accumulator arrays start at 0.
for line in range (cnt):
qtyarray.append(0)
tprarray.append(0)
# process each record in the file
for line in infile:
type, qty = eval(line)
tp = qty * nprlst[type-1]
qtyarray[type-1] = qtyarray[type-1] + qty
tprarray[type-1] = tprarray[type-1] + tp
infile.close
return qtyarray, tprarray
def prtbody(names, qtyarray, tprarray,nprlst, cnt):
#
#This function prints the body of the reprot
#
# Variable Type Purpose
# names string list holds tree names
# qtyarray int list quantity accumulator list
# nprlst int list holds the tree price
# tprarray int list price accumulator list
# x int used as a subscript
# cnt int holds the number of tree types
x=0
#
# Print title and column headings
#
print string.center ("Program 10")
print
print
print " Book Total Quantity Total"
print " Name Sold Sales"
print
#
# This prints the name left justified and then the next print statement
# prints the rest of the line.
#
for x in range (cnt):
print string.ljust(names[x], 18),
print "%4d $%6.2f $%8.2f" \
% (qtyarray[x],nprlst[x],tprarray[x])
def calc(qtyarray, tprarray, cnt):
#
#This function calculates the grand total sales and quantity.
#
# Variable Type Purpose
# qtyarray int list quantity accumulator list
# tprarray int list price accumulator list
# x int used as a subscript
# gtqty int holds grand total quantity
# gtsales int holds grand total sales
# cnt int holds the number of tree types
x=0
gtqty = 0
gtsales = 0
for x in range (cnt):
gtqty = gtqty + qtyarray[x]
gtsales = gtsales + tprarray[x]
return gtqty, gtsales
def main():
#
#This is the main function. It calls the other functions and prints the
# grand totals at the end of the report.
#
# Variable Type Purpose
# mcnt int counts the number
# mnames string list holds tree names
# mnprlst int list holds the tree price
# mqtyarray int list quantity accumulator list
# mtprarray int list price accumulator list
# mgtqty int holds grand total quantity
# mgtsales int holds grand total sales
mcnt, mnprlst, mnames = lnamelist()
mqtyarray, mtprarray = laccums(mcnt, mnprlst)
prtbody,(mnames, mqtyarray, mtprarray,mnprlst, mcnt)
mgtqty, mgtsales = calc(mqtyarray, mtprarray, mcnt)
print
print " %18s %4d %14s $%8.2f" % ("Total Quantity Sold ", mgtqty,"Total Sales ", mgtsales)
if __name__ == '__main__':
main()