CS student here...Maybe it's due to lack of sleep but I cannot figure out why my counts are not showing up at all. The formatting is correct but I cannot figure out why no count is displayed. Any help is appreciated.
Description:
Reads a small business's customer information from the text file: customerData.txt into a customerList. Each customer record is on a single line in the text file and generates one entry into the customerList which happens to be a list of 12 string fields. The order of the fields are First Name, Middle Initial, Last Name, Street Address, City, State, Zip Code, Country, Email Address, Telephone Number, Gender, and Birthday.
The customerList is used to generate a dictionary containing a tally of the number of customers from each state.
This dictionary is printed in two orders: sorted by state code, and sorted by state count.
Current Code:
def main():
""" Open's file, reads customer information into a list, closes the file"""
custFile = open('customerData.txt','r')
customerList = generateList(custFile)
custFile.close()
statesCountDictionary = generateStatesCountDictionary(customerList)
printSortedByState(statesCountDictionary)
printSortedByCount(statesCountDictionary)
def generateList(custFile):
""" Reads customer data from file and returns a list of customers"""
customers = []
for line in custFile:
custInfoList = line.strip().split(',')
customers.append(custInfoList)
return customers
def generateStatesCountDictionary(customerList):
""" Tallies the number of customers from each state and
returns a dictionary with the state as a key and the
count as the associated value."""
statesTallyDict = {}
for customer in customerList:
state = customer[5]
if state in statesTallyDict:
count = statesTallyDict.get(state) +1
else:
count = 1
return statesTallyDict
def printSortedByState(statesCountDictionary):
""" Prints the tally of the number of customers from each state
sorted alphabetically by the state abbreviation."""
tupleList = statesCountDictionary.items()
tupleList.sort()
print "\n\n%5s %-14s" % ("State","Customer Count")
print "-"*25
for item in tupleList:
state, count = item
print "%5s %8d" % (state.center(5), count)
def printSortedByCount(statesCountDictionary):
""" Prints the tally of the number of customers from each state
sorted from most to least."""
stateCountTupleList = statesCountDictionary.items()
countStateTupleList = []
stateCountTupleList.sort()
print "\n\n%5s %-14s" % ("Customer Count","State")
print "-"*25
for item in stateCountTupleList:
count, state = item
print "%8d %5s" %(count.center,state(5))
main()