gpt4 book ai didi

python - 类型错误 : 'NoneType' object is not iterable

转载 作者:太空狗 更新时间:2023-10-30 02:23:10 26 4
gpt4 key购买 nike

我在“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()

最佳答案

您尝试将 displayCost 的返回值解包为 4 个变量,但 displayCost 没有返回任何内容。因为每个函数调用在 Python 中都会返回一些东西(或抛出异常),所以不会返回 None。那么 None 是无法解包的。

你可能想改变:

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

收件人:

displayCost(companyName, monthAverage, costPerTon, totalCost)

关于python - 类型错误 : 'NoneType' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5784582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com