# initially we declare a variable that will hold the current sum
# and at the end of the for it will hold
# the sum of the numbers you have mentioned.
b = 0
for i in range(1000):
# Then whether i is evenly divided by 3 or 5
# we add this number to the current sum
if i % 3 == 0 or i % 5 == 0:
b += i
# last we print the sum
print b
你可以这样做:
从您的代码中,我想您需要可以被
3 or 5
整除的数的总和。在所有解决方案中,另一种可能的单线性解决方案是:这里您得到这个
TypeError
,因为sum
函数接受一个数字序列并返回序列的和。在您的代码中,传递i
作为sum
的参数,在您的例子中,它是一个int
类型的对象。所以你得到了这个错误。你知道吗使用以下行
sum以iterable对象作为输入。列表、集合等是可编辑的。你知道吗
所以试试下面的方法
相关问题 更多 >
编程相关推荐