![](/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.