0

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!!

2
  • 3
    Your indentation is wrong: column += 1 must be inside the inner while loop, and row += 1 must be inside the outer while loop. Because of incorrect indentation, column is never incremented and the inner while infloops. Commented Jul 3, 2014 at 21:10
  • This isn't a fix for your code, but rather an answer to the almost language-agnostic question in your title/first paragraph. A for loop in Python is really a for...each loop. It reads "for each X in Y" and gives you access to X to read or change it. A while loop reads "while X is True" and is by its nature infinite: it only stops executing when the state changes in a predetermined way. Commented Jul 3, 2014 at 21:14

3 Answers 3

2

Your column increment statement (column += 1) is outside the corresponding while loop.

Same for the row increment statement.

Right now, column will increment after the loop is done. But, you need it to increment after every iteration of the loop.

Add a level of intend to both these statements and you are good to go.

        Box[r_ow][c_colu] = str(int(box[r_ow][c_col]) + ctr)
        column += 1
    row += 1
return board    

Also there is a problem with your innermost while loop. neighbors is a list (or something similar) while ctr is an int. You can't compare the two. Also, ctr is not the variable iteration through neighbors. It's a counter. Do something like this.

i = 0
ctr = 0
while i < len(box): 
    if eval(Box(boxWidth,boxHeight,box_edges,box_point)):
       if box_edge in gridList:
            ctr += 1
    i+=1
1
  • You guys are great! I finally got out of the infinite loop!!
    – SteveZrg
    Commented Jul 3, 2014 at 21:27
1

You want your while loop increment to be inside the loop. It needs to be tabbed into the body of the loop. The problem is your row and column increments occur outside the loop, so they do not change while in the body. That makes an infinite loop.

0

If I understand you correctly, then your question is essentially about the built-in function range.

You can read all about this function here, but in short, it generates an array of numeric values.

Then, the line for i in range(...) essentially means:

For each element i in the array generated by range, do...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.