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 should unindent the last <code>if</code> statement, which runs after each digit:</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>As another answer notes, this can give you false duplicates if there is match in a number with trailing 0's. There is an added danger, though, of <em>false positives</em> if there is a number whose left X digits equal it, but whose total digits do not.</p>

python python-3.x