1
def armstrong(x):
    number = str(x)
    length = len(number)
    sum=0
    for i in number:
        sum = sum + pow(int(i),length)

    print("Yes {0} is an Armstrong Number" .format(x) if(sum==x) else print("No {0} is not an Armstrong Number" .format(x)))   

    return 0


num=int(input("Enter a Number: "))
armstrong(num) 

For a non armstrong number the output comes along with a None, could someone please explain the reason behind it. I've attached the output as an image link for the same.

log

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62
Dev
  • 21
  • 4
  • 2
    Remove the `print()` from the `else` and just have the string be the else-value in the `if ... else` operator. In any event -- why not make `armstrong()` return a Boolean values and let the calling code be responsible for printing? That would be a more useful function and a cleaner design. – John Coleman Oct 07 '21 at 10:13

1 Answers1

6

You have a print within another print, and since print always returns None, that's what you're printing. I'm guessing this is what you need:

print("Yes {0} is an Armstrong Number".format(x) if(sum==x) else "No {0} is not an Armstrong Number".format(x))

P.S try not to use the word sum because you're shadowing a built-in function

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62