![](/img/trans.png)
[英]I have this code and I don't get why the result of print(su) isn't zero
[英]I don't get a print result of function when using range
为什么我在使用range()
时看不到函数的结果? 我想创建一个数字范围,其中每个数字都将在“colla”函数中进行评估。 但是范围不适用于“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!
您的colla()
函数需要输入 2 及以上的数字。
尝试这个:
for i in range(2,6):
print(colla(i))
请注意,调用colla(0)
将导致无限循环,或者当fin
填满0
时“内存不足”。 同样调用colla(1)
会导致ZeroDivisionError
因为fin
将是一个空列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.