阿姆斯特朗号的条件

时间:2015-11-07 17:45:15

标签: python-2.7

有人可以告诉我我的代码有什么问题可以检查一个号码是否是阿姆斯特朗号码吗?

n=input('Enter the number=')
m=n
s=0
while n>0:
    d=n%10
    s=s+d**3
    n=n*10
if m==s:
    print'The number is an Armstrong number'
else:
    print'The number is not an Armstrong number'

2 个答案:

答案 0 :(得分:0)

我最终得到了该计划。结果我一直在键入n=n/10语句为n=n*10。有时会发生错误:)

n=input('Enter the number=')
    m=n
    s=0
    while n>0:
        d=n%10
        s=s+d**3
        n=n/10
    if m==s:
        print'The number is an Armstrong number'
    else:
        print'The number is not an Armstrong number'

答案 1 :(得分:0)

sum = 0
no = int(raw_input("Enter the the number to check it is armstrong on or not :"))
pow_no = len(str(no))
check = no

print "#################Method -I Result##############"
while 0<no:
  sum = sum +((no%10)**pow_no)
  no = no / 10
if check == sum:
  print "%s is Armstrong"%check
else:
  print "%s is not Armstrong"%check

print "################Method -II Result#############"
for i in str(no):
  sum = sum + (int(i)**pow_no)
if check == sum:
  print "%s is Armstrong"%check
else:
  print "%s is not Armstrong"%check