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

Issue

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!

Solution

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.

Answered By – quamrana

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.