擅长:python、mysql、java
<p>你有一个无限循环,因为<code>i</code>不是递增的,而且<code>result</code>。你知道吗</p>
<p>然后归结为你到底想要什么:</p>
<h2>总和小于1000</h2>
<pre><code>result = 0
i = 0
while result <= 1000:
i += 1
if i % 3 == 0 or i % 5 == 0:
result += i
print(result - i)
# 998
</code></pre>
<h2>元素小于1000</h2>
<p>如果您引用的是<a href="https://projecteuler.net/problem=1" rel="nofollow noreferrer">Euler problem</a>,那么总和不应小于1000,但是元素:</p>
<pre><code>total_sum = 0
for i in range(1000):
if (i % 3 == 0 or i % 5 == 0):
total_sum = total_sum + i
print total_sum
# 233168
</code></pre>
<p>另一种选择是:</p>
<pre><code>sum(set(range(0,1000,3)) | set(range(0,1000,5)))
# 233168
</code></pre>
<p>或:</p>
<pre><code>sum(range(0,1000,3)) + sum(range(0,1000,5)) - sum(range(0,1000,15))
# 233168
</code></pre>