5
我是一個初學者,使用Python並嘗試幾個程序。我有一些像Python中的以下WHILE循環結構(不是確切的)。在Python中嵌套的WHILE循環
IDLE 2.6.4
>>> a=0
>>> b=0
>>> while a < 4:
a=a+1
while b < 4:
b=b+1
print a, b
1 1
1 2
1 3
1 4
我期待外循環來遍歷1,2,3和4。我知道我能做到這一點FOR循環這樣
>>> for a in range(1,5):
for b in range(1,5):
print a,b
1 1
1 2
.. ..
.. .. // Other lines omitted for brevity
4 4
但是,什麼是錯的WHILE循環?我想我錯過了一些顯而易見的東西,但無法辨認出來。
答: 校正WHILE循環..
>>> a=0
>>> b=0
>>> while a < 4:
a=a+1
b=0
while b<4:
b=b+1
print a,b
1 1
.. ..
.. .. // Other lines omitted for brevity
4 4
P.S:搜索出SO,找到few questions但沒有接近這一點。不知道這是否可以歸類爲家庭作業,實際的程序是不同的,問題在於我困惑。