Nested While loop in Python
Asked Answered
R

5

3

I am a beginner in python programming. I wrote the following program but it doesn't execute as I want it to. 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 somebody help me?? I will be grateful indeed! Regards, Gillani

Ringler answered 14/9, 2009 at 12:43 Comment(5)
What do you want it to do? Explain morePeacock
What is the output? What did you expect it to be?Bribe
what do you want with the code?Vida
What is p? What is k? Do you want it to re-enter the inner loop? If so you need to reset x to 0 at the top of the outer loop.Poodle
Not a question, vote to close.Strainer
L
26

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

Your whole code doesn't look remotely like Python code ... loops like that are 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
Liberec answered 14/9, 2009 at 12:48 Comment(2)
range(0,11) is better written range(11). Zero is the default lower bound.Schelling
Even better is to use xrange(11). range creates a whole list and returns it to the caller. xrange returns a generator function, which delays allocation of elements until they are requested. For a 16 element array, it likely isn't a huge difference. However, if you are counting to 10,000, then xrange is definitely better.Sloganeer
B
14

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

To fix this move the 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 simpler way with simple bounds such as this is to use for loops:

for b in range(11):
  print b
  for x in range(16):
   print x
Burdine answered 14/9, 2009 at 12:57 Comment(0)
M
0

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

'p' is not defined 

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

Removing that that line lets the code run with output 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
>>>
Mandal answered 14/9, 2009 at 12:55 Comment(0)
L
0

You need to reset your x variable after processing the inner loop. Otherwise your outerloop will run through without firing 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
Lipinski answered 9/8, 2016 at 17:56 Comment(0)
I
0

If you were intending to test the number of iterations of each loop in a nested while loop and compare it with that of a for loop, I have just the program for that! Here you go:

#Nested while loop
i=5
j=5
count=0
count1=0
while i>0:
    count+=1
    print("\t\t",count,"OUTER LOOP")
    while j>0:
        count1+=1
        print(count1,"INNER LOOP")
        j-=1
    i-=1


#Nested for loop
count=0
count1=0
for i in range(5):
    count+=1
    print("\t\t",count,"OUTER LOOP")
    for j in range(5):
        count1+=1
        print(count1,"INNER LOOP")

I am attaching the output for your reference. This code was run on Python version 3.10

OUTPUT OF NESTED WHILE LOOP [OUTPUT OF NESTED FOR LOOP][2]

1: https://i.sstatic.net/ixM2k.png [2]: https://i.sstatic.net/1vsZp.png

EDIT: Nested while loops in Python works EXACTLY like nested for loops in Python. I have edited my program in the following way:

#Nested while loop
i=5
j=5
count=0
count1=0
while i>0:
    count+=1
    print("\t\t",count,"OUTER LOOP")
    while j>0:
        count1+=1
        print(count1,"INNER LOOP")
        j-=1
        break #Adding jump statement stops the iteration of inner loop and the outer loop gets executed again.
    i-=1

OUTPUT OF EDITED WHILE LOOP PROGRAM

Ingrain answered 20/3, 2023 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.