Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'NoneType' object is not iterable

Tags:

python

I get the "TypeError: 'NoneType' object is not iterable" on the line "companyName, monthAverage, costPerTon, totalCost = displayCost(companyName, monthAverage, costPerTon, totalCost)" and I can't for the life of me figure out why.

Sorry I can't seem to get it to format correctly...

4-25-11 Final Project, Global Garbage Program

the main function

def main():

    #intialize variables
    endProgram = 'no'
    companyName = 'NONAME'

    #call to input company name
    companyName = inputName(companyName)

    #start 'end program' while loop
    while endProgram == 'no':

        #initializes variables
        companyName = 'NONAME'
        monthAverage = 0
        costPerTon = 0
        totalCost = 0
        yearTotal = 0

        #call to input company name
        companyName = inputName(companyName)

        #call to get the tonnage
        yearTotal, monthAverage = getTonnage(yearTotal, monthAverage)

        #call to calculate the cost
        monthAverage, costPerTon, totalCost, yearTotal = calculateCost(monthAverage, costPerTon, totalCost, yearTotal)

        #call to display the cost
        companyName, monthAverage, costPerTon, totalCost = displayCost(companyName, monthAverage, costPerTon, totalCost)

        endProgram = raw_input('Do you want to end the program? (enter yes or no)')

gets the company name

def inputName(companyName):
    companyName = raw_input('What is the name of your company? ')
    return companyName

gets the tonnage

def getTonnage(yearTotal, monthAverage):
    yearTotal = input('How many tons of garbage are you dumping this year? ')
    monthAverage = yearTotal / 12
    return yearTotal, monthAverage

calculates the call

def calculateCost(monthAverage, costPerTon, totalCost, yearTotal):
    if monthAverage > 100:
        costPerTon = 7000
    elif monthAverage > 50:
        costPerTon = 6500
    elif monthAverage > 20:
        costPerTon = 5500
    else:
        costPerTon = 4500
    totalCost = costPerTon * yearTotal
    return monthAverage, costPerTon, totalCost, yearTotal

prints the cost

def displayCost(companyName, monthAverage, costPerTon, totalCost):
    print 'Dear',companyName
    print 'The average tonnage per month is ', monthAverage
    print 'The cost per ton is $',costPerTon
    print 'The total the is the cost per ton times total tons $',totalCost

runs main

main()
like image 622
Benn Avatar asked Jan 19 '23 21:01

Benn


1 Answers

You try to unpack the return value of displayCost into 4 variables, but displayCost doesn't return anything. Because every function call returns something in Python (or throws an exception), None is returned. Then None can't be unpacked.

You probably want to change:

companyName, monthAverage, costPerTon, totalCost = displayCost(companyName, monthAverage, costPerTon, totalCost)

To:

displayCost(companyName, monthAverage, costPerTon, totalCost)
like image 147
Thomas Edleson Avatar answered Jan 31 '23 17:01

Thomas Edleson