Search code examples
pythonnested-loops

Python Nested Loop Working Weirdly


I have a little problem in python, I wrote a nested loop, and when I executed my script, my second loop totally ignored the first loop...

    while 1 <= 5:
        for num in range(1, 10):
            print "test"
            i +=1
        k+=1

Solution

  • I think you intend to use k for control of while loop. Change to this:

    # you can give k or i a default value here as you like
    k = 0
    i = 0
    while k <= 5:
        for num in range(1, 10):
            print "test"
            i +=1
        k+=1