小编Mat*_*ton的帖子

列表输出被复制

我创建了一个简单的递归函数,用于确定数字是偶数还是奇数,对它们执行数学运算,并将它们附加到列表中:

iter_list = []
def function(n):
    iter_list.append(n)
    if n < 2:
        print iter_list
        print iterations
        pass
    elif n % 2 == 0:
        even = n / 2
        iter_list.append(even)
        collatz(even)
    elif n % 2 == 1:
        odd = (3 * n) + 1
        iter_list.append(odd)
        collatz(odd)
    else:
        print "Code not working."
        pass

stdin = input("Number")
print collatz(stdin)
iterations = len(iter_list) - 1
Run Code Online (Sandbox Code Playgroud)

当我用276运行该函数时,输出为:

Number[276, 138, 138, 69, 69, 208, 208, 104, 104, 52, 52, 26, 26, 13, 13, 40, 40, 20, 20, …
Run Code Online (Sandbox Code Playgroud)

python list output

1
推荐指数
1
解决办法
24
查看次数

标签 统计

list ×1

output ×1

python ×1