I am a beginner to Python, and my professor does poor job explaining the differences between loops. I wanted to ask this community the differences between For loops and While loops. I looked at various resources but what I am confused about is how for loops have no counter to bring them back to the beginning like while loops: I have the following code written in for() loop but my goal is to change it to a while loop. The code is not what is important is how to change this code to a While() loop.
for r_ow in range(Height_box):
for c_col in range(Width_box):
gridpoint = box * row + column
if gridpoint in gridList:
box[r_ow][c_col] = "Inside Box"
else:
outsideBox = (CurrentBox(boxWidth,Boxedge))
ctr = 0
for Box_edges in Box:
if eval(Box(boxWidth,boxHeight,box_edges,box_point)):
if box_edge in gridList:
ctr += 1
Bow[r_ow][c_col] = str(int(box[r_ow][c_col]) + ctr)
So far, I have gotten to a point where I think this is what it suppose to look like but now i get stuck in a CMD infinite loop.
row = 0
while r_ow < boxHeight:
column = 0
while c_col < boxWidth:
gridpoint = box * row + column
if gridpoint in gridList:
box[r_ow][c_ol] = "Inside Box"
else:
outsideBox = (CurrentBox(boxWidth,Boxedge))
ctr = 0
while ctr < Boxes:
if eval(Box(boxWidth,boxHeight,box_edges,box_point)):
if box_edge in gridList:
ctr += 1
Bow[r_ow][c_col] = str(int(box[r_ow][c_col]) + ctr)
column += 1
row += 1
return box
Can anyone give some advice as to how to format the first code into while loops()?
thank you very much!!
column += 1
must be inside the innerwhile
loop, androw += 1
must be inside the outerwhile
loop. Because of incorrect indentation,column
is never incremented and the innerwhile
infloops.for
loop in Python is really afor...each
loop. It reads "for each X in Y" and gives you access to X to read or change it. Awhile
loop reads "while X is True" and is by its nature infinite: it only stops executing when the state changes in a predetermined way.