n=int(input("Enter a Number: "))
x=0
y=0
z=0
while(n>0):
x=n%10
y=x**3
z=z+y
n=n//10
print (z)
#The z here is the same value which I enter, yet it doesn't work.
#If I enter 407 as n, z becomes (4^3)+(0^3)+(7^3) which is 407
if (z==n):
#But even when 407==407, it just wont print the bottom statement
print ("The number is Armstrong")
else:
print ("The number isn't Armstrong")
#it prints that it isn't an Armstrong number
不使用任何内置方法
阿姆斯特朗数是
^{pr2}$371
,因为3**3 + 7**3 + 1**3 = 371
。根据这个规则,123
不是阿姆斯特朗数,因为1**3 + 2**3 + 3**3 is not equal to 123
在
while
循环之后,n
已经变成了4//10
,即0
,因此它永远不会等于407。在您需要保留原始输入的副本以供比较。在
{或者至少把cd6>作为一个通用的调试程序使用。在
你可以把你的初始数字作为一个字符串,这样我们就可以更容易地把它转换成一个列表。然后我们可以}的列表。然后我们可以使用列表理解将该列表中的所有
map
来创建{int
提升为列表的len
的幂次。如果这个列表的sum
等于我们的输入,那么我们有一个Armstrong数。在扩展列表理解:
^{pr2}$替代
^{3}$map
:相关问题 更多 >
编程相关推荐