我编写冰雹序列的编码导致循环不断

时间:2018-12-30 08:46:13

标签: python python-3.x

我正在尝试构造冰雹序列,为什么它不能结束循环并输出结果?

def hailstone(n):
    """
    print the hailstone sequence starting at n and return its length
    """
    hail=[n]
    while n>0:
        if n%2==0:
            n=n/2
            hail.append(n)
            #n=n/2
        elif n%2==1:
            n=n*3+1
            hail.append(n)
            #n=n*3+1
    return hail

something like this:
 >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7

1 个答案:

答案 0 :(得分:-2)

def冰雹(n):     “”     打印从n开始的冰雹序列并返回其长度

  
    
      

a =冰雹(10)           10           5           16           8           4           2           1个       一种           7           “”           计数= 1           而n> = 1:               如果n == 1:                   打印(n)                   打破               elif n%2 == 0:                   打印(n)                   计数+ = 1                   n = n // 2               其他:                   打印(n)                   计数+ = 1                   n = 3 * n + 1           返回计数

    
  

a =冰石(10) 一种 b =冰石(27) b

记住要设置休息时间