gpt4 book ai didi

python - 尝试分配循环索引时“int”对象不可迭代

转载 作者:行者123 更新时间:2023-11-28 20:36:20 25 4
gpt4 key购买 nike

<分区>

这真的很奇怪,请在下面的代码片段中进行循环,当我删除 num=i 时,一切正常,但它会导致迭代问题。

错误代码

In [17]: prim_list=[]
...: for i in range(2,101):
...: print i
...: num=i,count=0 # line with issue
...: for j in range(1,i/2):
...: div=j
...: if(num%div==0):
...: count=count+1
...: if(count==1):
...: prim_list.append(num)
...:
2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-a8dd2a52322e> in <module>()
2 for i in range(2,101):
3 print i
----> 4 num=i,count=0
5 for j in range(1,i/2):
6 div=j

TypeError: 'int' object is not iterable

工作正常

In [19]: prim_list=[]
...: for i in range(2,101):
...: count=0 # notice num=i assignment is removed
...: for j in range(1,i/2):
...: div=j
...: if(num%div==0):
...: count=count+1
...: if(count==1):
...: prim_list.append(num)
...: print "done"
...:
done

我不明白的是为什么赋值导致循环迭代错误!!??

感谢任何帮助!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com