2014-02-21 148 views
0

有人能告诉我为什么这个嵌套循环只执行内部while部分而不是8次吗?嵌套循环不能正常工作

corCols = 10 
corRows = 8 
cCount = 0 
for rCount in range(corRows): 
    while cCount < corCols: 
     print "***" + str(cCount) 
     cCount += 1 
    print "###" + str(rCount) 

这将打印在第一次迭代通过列,然后通过行迭代,但似乎只执行一次,而部分?

+0

这是* *运行10次,第一次'for'环运行。 –

+2

你的意思是问为什么它没有运行80次? –

+0

cCount不再小于corCols。顺便说一句,有一些严重的标识符名称。 – 2014-02-21 17:14:50

回答

2

您需要的内环

1

你的内循环运行只是一次前cCount复位至零,为cCount递增到10首次for循环迭代。其后cCount停留10while条件总是False

如果你想要的while循环,再次为for循环的每个迭代运行,在for环复位cCount

corCols = 10 
corRows = 8 
for rCount in range(corRows): 
    cCount = 0 
    while cCount < corCols: 
     print "***" + str(cCount) 
     cCount += 1 
    print "###" + str(rCount)