Python循环控制
相当于什么Python循环控制,python,Python,相当于什么 for (I=2; I<n; I*=2) for(I=2;ISimple是python中的规则。虽然很简单: n = 20 i = 2 while (i<n): print "the i variable is "+str(i) i = i * 2 n=20 i=2 while(iSimple)是python中的规则。while很容易: n = 20 i = 2 while (i<n): print "the i variable i
for (I=2; I<n; I*=2)
for(I=2;ISimple是python中的规则。虽然很简单:
n = 20
i = 2
while (i<n):
print "the i variable is "+str(i)
i = i * 2
n=20
i=2
while(iSimple)是python中的规则。while很容易:
n = 20
i = 2
while (i<n):
print "the i variable is "+str(i)
i = i * 2
n=20
i=2
while(iwhilewhile
很简单,while
也很慢。试试:
In [3]: import math
In [4]: n = 10
In [5]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
In [6]: n = 40
In [7]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
16
32
In [8]: n = 60
In [9]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
16
32
In [10]: n = 100
In [11]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
16
32
64
虽然While
很容易,但While
也很慢。请尝试:
In [3]: import math
In [4]: n = 10
In [5]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
In [6]: n = 40
In [7]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
16
32
In [8]: n = 60
In [9]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
16
32
In [10]: n = 100
In [11]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
16
32
64
这是一个无限循环。<代码> 0×2 < /> >是代码>错误>你的代码,错误是你得到的,你希望得到什么结果。这将帮助我们帮助你。这是java或C++的语法。我想在Python中做这是一个无限循环。<代码> 0 * 2 < /COD>是代码> 0 < /C>。显示你的代码,你得到的错误和你期望的结果。这是帮助我们帮助你的。这是java或C++的语法。我想用Python来做。问题是i=2,这是乘法,i=i+2是你的答案。注释指出,概念是被教的;我写了真正的金钱代码。好,我想撤销投票,但是我不能这样做,除非你编辑你的答案。命令您按照客户的要求去做,而不是写下您想要的内容,并告诉他可以修改它以满足他/她想要的工作要求。:-)问题是i=2,这是乘法,i=i+2在你的答案是加法。注释指出,概念正在被教导;我为金钱写真实的代码。好,我想撤销表决,但是我不能这样做,直到你编辑你的答案。考虑你可以做什么,客户要求,而不是写你想要的,告诉他。他可以修改它,以使他能按照他/她想要的那样工作。- -请考虑,在Python 2中,1/2=0,因此0.5可以更一般;加上N**(0.5)不是你需要的。你应该使用Log2。“QueNoMouth-Cyt再次,你绝对正确。我已经再一次编辑了它。它是那些早晨……>导入这个;)请考虑,python2中的1/2=0,因此0.5可以更一般;加上n**(0.5)不是您需要的。你应该使用log2。@quantummind,你完全正确。我又编辑了一遍。这是一个早晨…>>>>导入此;)