lst = []
def is_armstrong_number(number):
sum_num = 0
for digit in str(number):
lst.append(int(digit))
for i in lst:
i = pow(i, len(lst))
sum_num += i
if sum_num == number:
return True
else:
return False
This code doesn’t work for some numbers. I think the issue lies with the nested loops – they might be preventing the code from completing its calculations properly.
Could you walk through the code execution when that code is called with the input 10? Which lines execute in which order and what is the value on each variable on the line as the line is executed?
lst = []
def is_armstrong_number(number): #10
sum_num = 0
for digit in str(number): # it should be 1 and 0, but in IDE it just ouputs 1
lst.append(int(digit)) #expected : [1,0] , redult I get : [1]
for i in lst:
i = i ** len(lst) # 1 ** 2 = 1 , 0 ** 2 = 0
sum_num += i # 1 ** 2 + 0 ** 2 = 1 => 1 != 10
if sum_num == number: 1 != 10
return True # skipped
else:
return False # runs