我为一个快乐的号码检查器编写的代码不起作用,有人可以检查一下吗?

时间:2012-09-16 14:46:47

标签: python python-2.7 set

a = int(raw_input("Enter a number to check if it is happy:"))
l = map(int, str(a))
c = 0
while 1:
    for i in l:
       b = i
       c = c + b **2
    if c == 1:
        print "Happy Number!"
        raw_input("Hit enter to end")
        break
    elif c == a:
        print ("Not a Happy number")
        raw_input("Hit enter to end")
        break
    else:
        l = map(int, str(c))

这是我用Python编写的代码,它使用数字1和0,但是我尝试过的任何其他数字都会使它无限循环。有人可以看看我做错了什么并提出解决方案吗?谢谢:))

供参考:http://en.wikipedia.org/wiki/Happy_number

6 个答案:

答案 0 :(得分:6)

试试这个:

a = int(raw_input("Enter a number to check if it is happy: "))
visited = set()
while 1:
    if a == 1:
        print "Number is happy!"
        break
    a = sum(int(c) ** 2 for c in str(a))
    if a in visited:
        print "Number is sad!"
        break
    visited.add(a)

答案 1 :(得分:3)

您应该保留在此过程中生成的所有值:

def is_happy_number(n):
     seen = set()
     while True:
           digits = [int(c) for c in str(n)]
           n = sum(digit**2 for digit in digits)
           if n == 1:
                return True
           elif n in seen:
                return False
           seen.add(n)

>>> is_happy_number(1)
True
>>> is_happy_number(7)
True
>>> is_happy_number(11)
False

那是因为如果你有一个循环,那并不意味着循环将从你开始的地方开始。 例如:

11 -> 2
2 -> 4
4 -> 16
16 -> 37
37 -> 58
58 -> 89
89 -> 145
145 -> 42
42 -> 20
20 -> 4

正如您所看到的,您以“11”开头,但循环以“4”开头。

答案 2 :(得分:2)

在while循环中放置一个print语句

while 1:
    print(l)

并查看用户输入时发生的情况,例如2

您需要重置c = 0

else:
    l = map(int, str(c))
    c = 0

并且不愉快的数字的条件也需要改变。一旦你输入调试打印语句,我想你会看到需要做什么。

答案 3 :(得分:1)

有2个问题,你需要重置“c”,它几乎不会自行循环。试试这个:

a = int(raw_input("Enter a number to check if it is happy:"))
l = map(int, str(a))
while 1:
    c = 0
    for i in l:
       b = i
       c = c + b **2
    if c == 1:
        print "Happy Number!"
        raw_input("Hit enter to end")
        break
    elif c == 4:
        print ("Not a Happy number")
        raw_input("Hit enter to end")
        break
    else:
        l = map(int, str(c))

我移动了“c = 0”命令并调整了elif语句

答案 4 :(得分:0)

为什么如果elif c == a:? 维基:“如果n不满意,那么它的顺序就不会变为1.相反,它会在循环中结束”

试试这个:

r=int(raw_input("Enter Number :"))
happy=[]
for _ in range(r):
    number=_
    l=[]
    s=0
    while not (s in l[:-1]) and s!=1:
        s=0
        for __ in str(number):
            s+= int(__)**2
        l.append(s)
        number=s
    if l[-1]==1:

        happy.append(_)

print ("Happy Numbers are :")
print happy

答案 5 :(得分:0)

number=int(input("enter how many happy number to print"))
count=0
print("the first {} happy numbers are:".format(number))
for i in range(1,100000):#you can change the range if you want more happy numbers
    n=i
    while n!=1 and count!=number:
        num=n
        temp=0
        while num!=0:
            rem=num%10
            temp=temp+(rem**2)
            num=num//10
        n=temp
        if n==4:#condition to break the loop if it is no a happy number
            break    
    if n==1:
        count=count+1  
        print (i)