Python 2.7 阿姆斯特朗数的条件
有人能告诉我我的密码有什么问题吗?我的密码用来检查一个号码是否是阿姆斯特朗号码Python 2.7 阿姆斯特朗数的条件,python-2.7,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' 我终于让这个项目开始运作了。
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'
我终于让这个项目开始运作了。原来我一直在把语句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'
你预计会发生什么?究竟发生了什么?很明显,如果你有错误,这些都是帮助你的关键。我试图将数字分解成组成数字,但这并没有发生。至于错误,它没有显示任何错误语句,而是无限运行。@Marcus Müller无论如何感谢您的评论。这让我重新理性地思考了整件事:
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