Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Write a program that will read all this information for last month’s sales and summarize the total sales by salesperson by product. All totals should be stored in list sales. After processing all the information for last month, display the results
in tabu- lar format with each of the columns representing a particular salesperson and each of the rows repre- senting a particular product. Cross total each row to get the total sales of each product for last month; cross total each column to get the total sales by salesperson for last month. Your tabular printout should include these cross totals to the right of the totaled rows and to the bottom of the totaled col- umns.
Use a list of lists to solve the following problem. A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains:
a) The salesperson number.
b) The product number.
c) The number of that product sold that day.
```
# Using a list of lists to track sales.
# sales [ salesPerson ] [ units of product sold ]
# returns the total number of sales per product
def productSales( aList, product):
sum = 0
# sum units of a product sold by all salespeople
for salesPerson in range( len( sales ) ):
sum += aList[ salesPerson ][ product ]
return sum
# returns the total number of sales per person
def personSales( products ):
sum = 0
# sum all products sold by one sales person
for product in products:
sum += product
return sum
# main program
salesPeople = [ 1, 2, 3, 4 ] # valid salespeople values
products = [ 1, 2, 3, 4, 5 ] # valid product values
sales = [] # sales information
# initialize sales to 0
for salesPerson in range( len( salesPeople ) ):
sales.append( [ 0, 0, 0, 0, 0 ] )
print "This program calculates sales totals."
# user enters salesperson and product numbers
while 1:
salesPerson = int(
raw_input( "\nEnter salesperson (-1 to quit): " ) )
# user wants to exit
if salesPerson == -1:
break
# error handling for salesperson number
if salesPerson not in salesPeople:
print "Invalid salesperson. Valid salesperson numbers:", \
salesPeople
continue
product = int( raw_input( "Enter product number: " ) )
# error handling for product number
if product not in products:
print "Invalid product. Valid product numbers:", products
continue
# increment sales count (subtract 1 for subscripts)
sales[ salesPerson - 1 ][ product - 1 ] += 1
# print table header
print "\n%7s %21s %29s" % \
( "Product", "Salesperson", "Total Product Sold" )
print "%15s %6s %6s %6s" % ( "[1]", "[2]", "[3]", "[4]" )
# display sales information in tabular format
for product in range( len( products ) ):
# display product number under "Product" header
print "[%d] " % ( product + 1 ),
# display number of product sold by each salesperson
for salesPerson in range( len( salesPeople ) ):
print "%6d" % sales[ salesPerson ][ product ],
# display total sales of each product
print "%15d" % productSales( sales, product )
# display total number of sales per person
print "\nTotal: ",
for salesPerson in range( len( salesPeople ) ):
print "%6d" % personSales( sales[ salesPerson ] ),
print
```
You might also like to view...
What is the most common connector that is used with UTP cabling carrying Ethernet traffic?
A) RJ-48 B) RJ-45 C) RJ-11 D) RJ-14
Comments in Alice are instructions for the computer to take into account while it is executing a program.
Answer the following statement true (T) or false (F)