TypeError:'NoneType'对象不可迭代

时间:2011-04-26 00:05:45

标签: python

我在“companyName,monthAverage,costPerTon,totalCost = displayCost(companyName,monthAverage,costPerTon,totalCost)”一行中得到“TypeError:'NoneType'对象不可迭代”,我不能为我的生活找出原因。

抱歉,我似乎无法正确格式化...

11年4月25日 最终项目,全球垃圾计划

主要功能

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)')

获取公司名称

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

获得吨位

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

计算通话

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

打印费用

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

运行主

main()

2 个答案:

答案 0 :(得分:5)

您尝试将displayCost的返回值解压缩为4个变量,但displayCost不会返回任何内容。因为每个函数调用都在Python中返回一些东西(或引发异常),所以返回None。然后无法解压缩。

您可能想要更改:

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

要:

displayCost(companyName, monthAverage, costPerTon, totalCost)

答案 1 :(得分:2)

displayCost()不返回任何内容。

>>> a, b, c = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable