TypeError: 'NoneType'对象不可迭代,函数返回结果未定义
我在这一行代码“companyName, monthAverage, costPerTon, totalCost = displayCost(companyName, monthAverage, costPerTon, totalCost)”上遇到了“TypeError: 'NoneType' object is not iterable”的错误,我真搞不懂为什么会这样。
抱歉,我似乎无法正确格式化内容...
4-25-11
最终项目,全球垃圾项目
主函数
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 个回答
3
displayCost()
这个函数没有返回任何东西。
>>> a, b, c = None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
6
你试图把 displayCost 函数的返回值拆分成 4 个变量,但 displayCost 并没有返回任何东西。在 Python 中,每个函数调用都会返回一些东西(或者抛出一个异常),如果没有返回值,就会返回 None。所以 None 是不能被拆分的。
你可能想要把:
companyName, monthAverage, costPerTon, totalCost = displayCost(companyName, monthAverage, costPerTon, totalCost)
改成:
displayCost(companyName, monthAverage, costPerTon, totalCost)