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”
JavaScript
x
18
18
1
def colla(num):
2
fin = []
3
while num != 1:
4
if num % 2 == 0:
5
num = num // 2
6
fin.append(0)
7
elif num % 2 == 1:
8
num = 3 * num + 1
9
fin.append(1)
10
counter = []
11
for Z in fin:
12
if Z == 0:
13
counter.append(Z)
14
return ("{:.0%}".format((len(counter)/len(fin))))
15
16
for i in range(5):
17
print(colla(i)) # here I have a problem!
18
Advertisement
Answer
Your colla()
function requires input numbers of 2 and above.
Try this:
JavaScript
1
3
1
for i in range(2,6):
2
print(colla(i))
3
Please be aware that calling colla(0)
will result in an infinite loop, or rather “out of memory” when fin
fills up with 0
s. Also calling colla(1)
results in a ZeroDivisionError
because fin
will be an empty list.