def garden(seedList):
flower = [2, 5, 12]
flowers = []
for each in range(len(seedList)):
totalFlowers = flowers.append(seedList[each] * flower[each])
x = sum(totalFlowers)
return totalFlowers
我收到错误:
The error was:iteration over non-sequenceInappropriate argument type.An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer.
我需要解决的问题是:
编写一个函数,根据每种花的种子数计算花的总数。seedList参数将包含
有。每粒种子都会结出一定数量的花。一个矮牵牛花种子能产生2朵花,一个雏菊种子能产生5朵花。一粒玫瑰种子将产生12朵花。每种花的种子。seedList参数将包含
有。您应该返回一个整数,其中包含您花园中的花的总数。
最佳答案
问题是list.append
修改列表并返回None
。
totalFlowers = flowers.append(seedList[each] * flower[each])
所以你的代码实际上是:
x = sum(None)
代码的工作版本:
def garden(seedList):
flower = [2, 5, 12]
flowers = []
for each in range(len(seedList)):
flowers.append(seedList[each] * flower[each])
return sum(flowers)
使用
zip
的更好的解决方案:def garden(seedList):
flower = [2, 5, 12]
totalFlowers = sum ( x*y for x,y in zip(flower, seedList) )
return totalFlowers