0

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()
CFalco
  • 19
  • 5
  • Can you supply some sample input/output? – TemporalWolf Jun 24 '16 at 04:59
  • When you say "I cannot figure out why no count is displayed", do you mean that there is an error raised or absolutely nothing is `print`ed or it `print`s something unexpected? It also looks like you are parsing through a csv file. – Moon Cheesez Jun 24 '16 at 05:11

3 Answers3

1

In generateStatesCountDictionary you never actually add any values to the dictionary.

You've calculated the count now you need to set that state's value in statesTallyDict

user3220892
  • 227
  • 5
  • 10
0
    if state in statesTallyDict:
        count = statesTallyDict.get(state) +1
    else:
        count = 1

Isn't actually updating the count in the dictionary itself..

    if state in statesTallyDict:
        statesTallyDict[state] = statesTallyDict.get(state) + 1
    else:
        statesTallyDict[state] = 1

or, using the more pythonic

    if state in statesTallyDict:
        statesTallyDict[state] += 1
    else:
        statesTallyDict[state] = 1
TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
0

As user3220892 has said, you are not adding any values to the statesTallyDict dict:

def generateStatesCountDictionary(customerList):
    statesTallyDict = {}
    for customer in customerList:
        state = customer[5]
        if state in statesTallyDict:
            count = statesTallyDict.get(state) +1
        else:
            count = 1
    return statesTallyDict

To assign a key to a value in a dictionary, you would have to "index" the non-existant key and give it a value:

my_dict[key] = value

Similarly:

if state in statesTallyDict:
    statesTallyDict[state] = statesTallyDict.get(state) +1
else:
    statesTallyDict[state] = 1

However, in this case, you can use the default value for .get:

statesTallyDict[state] = statesTallyDict.get(state, 0) + 1
Community
  • 1
  • 1
Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38