[ACCEPTED]-Nested While loop in Python-while-loop

Accepted answer
Score: 26

Not sure what your problem is, maybe you 4 want to put that x=0 right before the inner 3 loop ?

Your whole code doesn't look remotely 2 like Python code ... loops like that are 1 better done like this:

for b in range(0,11):
    print 'here is the outer loop',b
    for x in range(0, 16):
        #k=p[x]
        print 'here is the inner loop',x
Score: 14

Because you defined the x outside of the 6 outer while loop its scope is also outside 5 of the outer loop and it does not get reset 4 after each outer loop.

To fix this move the 3 defixition of x inside the outer loop:

b = 0
while b <= 10:
  x = 0
  print b
  while x <= 15:
    print x
    x += 1
  b += 1

a 2 simpler way with simple bounds such as this 1 is to use for loops:

for b in range(11):
  print b
  for x in range(16):
   print x
Score: 0

When running your code, I'm getting the 4 error:

'p' is not defined 

which means that you are trying to 3 use list p before anything is in it.

Removing 2 that that line lets the code run with output 1 of:

here is the outer loop
0 here is the inner loop
0 here is the inner loop
1 here is the inner loop
2 here is the inner loop
3 here is the inner loop
4 here is the inner loop
5 here is the inner loop
6 here is the inner loop
7 here is the inner loop
8 here is the inner loop
9 here is the inner loop
10 here is the inner loop
11 here is the inner loop
12 here is the inner loop
13 here is the inner loop
14 here is the inner loop
15 here is the outer loop
1 here is the outer loop
2 here is the outer loop
3 here is the outer loop
4 here is the outer loop
5 here is the outer loop
6 here is the outer loop
7 here is the outer loop
8 here is the outer loop
9 here is the outer loop
10
>>>
Score: 0

You need to reset your x variable after 3 processing the inner loop. Otherwise your 2 outerloop will run through without firing 1 the inner loop.

b=0
x=0
while b<=10:
    print 'here is the outer loop\n',b,
    while x<=15:
        k=p[x] #<--not sure what "p" is here
        print'here is the inner loop\n',x,
        x=x+1
x=0    
b=b+1

More Related questions