快乐数问题说明

正常人的在编写这个程序时会遇到的一个问题就是非快乐数进入循环之后如何跳出循环。

以下是我解决问题的方法(详情请看源代码)

def  juge(n):
    add=0
    if  n=='1':
        return  True
    for  c  in  n:
        add=add+int(c)**2
    n=str(add)
    n=juge(n)
n=input()
try :
     juge(n)
except :
        print("{} is  not  a  happynumber !".format(n))
else :
    print("{} is  a  happynumber !".format(n))
    
        

  

另外再提供一种思路,详情请看下边图片。

.

猜你喜欢

转载自www.cnblogs.com/hbu-xiaopipi/p/9233360.html