任务是将3的倍数加到100以下,然后将它们加在一起。
num1 = 0
l = []
while num1 < 100:
num1 = num1 + 3
l.append(num1)
# I used this to delete the last element in the list which is 102,
del l[-1]
print l
# sum of all the numbers in l
b = sum(l)
print b
答案 0 :(得分:6)
total = 0
for i in range(0,100, 3):
total += i
print(total)
替代方案,单行:
answer = sum(range(0, 100, 3))
答案 1 :(得分:3)
你可以试试这个:
sum(range(0, 100, 3))
答案 2 :(得分:3)
通过高斯和,
total = 3 * (100 // 3) * (100 // 3 + 1) // 2