pythonlistloopsmathalgebra

Why my python Armstrong number code doesnt work


So ive done an armstrong number code using python and it works fine till the sum of individual digits but after that it doesnt work i will type the code and send a screenshot to show in picture.

number = int(input('Enter a number'))
n = len(str(number))
m = s = 0
list1 = []
for i in range(n):
    m = number % 10
    print(m)
    list1.append(m)
    number = number // 10
print(list1)

for o in list1:
    p = o ** n
    s = s + p
    print(s, 'is the sum of nth power of individual terms')

if int(s) == int(number):
    print('It is an armstrong number')
else:
    print('It is not an armstrong number')

enter image description here

I tried to program an armstrong number code for recreational purposes but it doesnt output the way i want it


Solution

  • Try this

    number = int(input('Enter a number: '))
    original_number = number
    n = len(str(number))
    s = 0
    list1 = []
    for i in range(n):
      m = number % 10
      list1.append(m)
      number = number // 10
    
    for o in list1:
      p = o ** n
      s = s + p
    
    if s == original_number:
      print('It is an Armstrong number')
    else:
      print('It is not an Armstrong number')