Python nested while loop

I'm starting to program in python. I wrote the following program, but it does not execute as I want. Here is the code:

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

Can someone help me? I will be grateful! Regards, Gilani

+1
source share
4 answers

Not sure what the problem is, maybe you want to put this one x=0right in front of the inner loop?

All your code doesn't look remotely like Python code ... loops like this are best done as follows:

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
+24
source

Since you defined x outside the outer while loop, its area is also outside the outer loop, and after each outer loop it does not get reset.

, x :

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

, , :

for b in range(11):
  print b
  for x in range(16):
   print x
+10

Running code. I get an error if "p" does not reinstall ", which means that you are trying to use the p array before anything in it.

Removing this line allows you to run code with exit

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
>>> 
0
source

You need to reset your x variable after processing the inner loop. Otherwise, your outer circle will work without starting the inner cycle.

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
0
source

All Articles