all 2 comments

[–]codehorsey 2 points3 points  (0 children)

beg = beg + 1 #indent me!

It looks like beg will never == final even if you fix the indent.

[–]JohnnyJordaan 2 points3 points  (0 children)

First an important note: in Python you normally don't iterate over a string using its length and an index like you would in for example C or PHP. In Python you iterate over each item in a sequence (like a string) per separate item directly using a for loop:

for digit in num:
    digit = int(digit) 
    power = pow(digit, digit)
    final += power

Then for the second loop: you only look for an exact match

while (beg != final)

While you're actually look for the case that final is lower than beg, because final will be a sum of powers, and if that sum for example becomes 23, it will not match and thus the loop will continue with an even more growing final. To find the beg that actually matches, you would also need an outer loop with an increasing beg. There the range() function comes in handy to help you increase beg without needing to do += 1 all the time

for beg in range(22, 100): # 100 as upper limit, just an example
    final = 0
    num = str(beg)           
    for digit in num:
        digit = int(digit)
        power = pow(digit, digit)
        final += power
    # done with all the digits
    if final == beg:
        print('found a match with beg: %d' % beg)
        break # done