[英]Lists, For Loops, Ranges, Indexes
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-sequence Inappropriate 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.
The error was:iteration over non-sequence Inappropriate 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.