Narcissistic number
<p>Today I saw an interesting task. To make a program which outputs all "narcissistic numbers" (all digits are raised to the power of 3). My program has this code</p>

<pre><code>for number in range(1, 408):
    result = 0
    for digit in str(number):
        result += int(digit) ** 3
        if result == number:
            print(number)
</code></pre>

<p>The output is</p>

<pre><code>1
153
370
370
371
407
</code></pre>

<p>Why does 370 appear twice?</p>

<p>You're checking whether <code>result==number</code> after each digit in the number. You probably want this check in the outer <code>for</code> loop. As it is, it sees that 370 = 3**3 + 7**3, but it is also 3**3 + 7**3 + 0**3, so it's printed on both of those iterations.</p>

python python-3.x