Skip to content
Advertisement

I don’t get a print result of function when using range

Why can’t I see the result of the function when I use range()? I want to create a range of numbers where each the number will be evaluate in “colla” function. But range doesn’t work with “colla”

def colla(num):
    fin = []
    while num != 1:
        if num % 2 == 0:
            num = num // 2
            fin.append(0)
        elif num % 2 == 1:
            num = 3 * num + 1
            fin.append(1)
    counter = []
    for Z in fin:
        if Z == 0:
            counter.append(Z)
    return ("{:.0%}".format((len(counter)/len(fin))))

for i in range(5):   
    print(colla(i)) # here I have a problem!

Advertisement

Answer

Your colla() function requires input numbers of 2 and above.

Try this:

for i in range(2,6):   
    print(colla(i))

Please be aware that calling colla(0) will result in an infinite loop, or rather “out of memory” when fin fills up with 0s. Also calling colla(1) results in a ZeroDivisionError because fin will be an empty list.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement