Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Outer loop not running
#1
Hi

I'm working on something and it has a nested while loop. It seems like only the inner loop is running and only once;

Code:

def answer(x, y):

    idbadge = [[0 for i in range(x)] for i in range(y)]

    row = 0
    col = 0
    a = 1

    #################Detemine number of cells###############
    count = 0
    counter = 0
    i=0
    a=0
    while (a < x):
        i = i + 1
        a = a + i
        count = i
        counter = i
        print("Count is " + str(count));
        print("Counter is " + str(counter))


    row = 0
    col = 0
    a = 1
    #n = 0
    ##############Build Matrix###############

    row = 0
    col = 0
    a = 1
    t = 2

    print("Count: " + str(count))


    while col < count:
        while row < count:

            print("Row:" + str(row))
            print("Col:" + str(col))
            print("ID BADGE" + str(a))
            idbadge[row][col] = a
            row = row + 1
            a = a + row

        col = col + 1
        #t = t + 1
        #a = t


    ###############Print Out###############

    #print top row (row 0)
    #print second top row
    #continue printing until row is equal to 4


answer(10,10)
Reply
#2
the outer loop has to run at least once for the inner loop to run.

Output:
outer iteration col col < count inner iteration count row row < count --------------- --- ------------ --------------- ----- --- ----------- 1 0 yes 1 4 0 yes 2 4 1 yes 3 4 2 yes 4 4 3 No 2 1 yes 1 4 4 No 3 2 yes 1 4 4 No 4 3 yes 1 4 4 No 5 4 no
so actually the outer loop is worthless
you need to reset row to zero in first loop after each inner loop has run its course
def answer(x, y):
 
    idbadge = [[0 for i in range(x)] for i in range(y)]
 
    row = 0
    col = 0
    a = 1
 
    #################Detemine number of cells###############
    count = 0
    counter = 0
    i=0
    a=0
    while (a < x):
        i = i + 1
        a = a + i
        count = i
        counter = i
        # print("Count is " + str(count));
        # print("Counter is " + str(counter))
 
 
    row = 0
    col = 0
    a = 1
    #n = 0
    ##############Build Matrix###############
 
    row = 0
    col = 0
    a = 1
    t = 2
 
    # print("Count: " + str(count))
 
 
    while col < count:
        while row < count:
 
            # print("Row:" + str(row))
            # print("Col:" + str(col))
            print("ID BADGE" + str(a))
            idbadge[row][col] = a
            row = row + 1
            a = a + row
        row = 0
        col = col + 1
        #t = t + 1
        #a = t
 
 
    ###############Print Out###############
 
    #print top row (row 0)
    #print second top row
    #continue printing until row is equal to 4
 
 
answer(10,10)
Output:
ID BADGE1 ID BADGE2 ID BADGE4 ID BADGE7 ID BADGE11 ID BADGE12 ID BADGE14 ID BADGE17 ID BADGE21 ID BADGE22 ID BADGE24 ID BADGE27 ID BADGE31 ID BADGE32 ID BADGE34 ID BADGE37
using list comprehension this code can be reduced to a few lines
Reply
#3
idbadge = [[0 for i in range(x)] for i in range(y)]
may be re-written as
id_badge = [[0] * columns for _ in range(rows)]
Using one-letter variable - in general - is one of the most successful methods to write unreadable code. Reading PEP-8 is recommended
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help RuntimeError: no running event loop marpaslight 5 3,712 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,882 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  bleak library RuntimeError: This event loop is already running alice93 3 4,080 Sep-30-2021, 08:06 AM
Last Post: alice93
  loop running indefinitely shantanu97 6 2,553 Sep-29-2021, 08:03 PM
Last Post: deanhystad
  Running A Loop Until You See A Particular Result knight2000 6 31,670 Sep-04-2021, 08:55 AM
Last Post: knight2000
  Running loop at specific frequency mdsousa 3 5,918 Apr-21-2021, 11:22 AM
Last Post: jefsummers
  RuntimeError: This event loop is already running newbie2019 2 6,950 Sep-30-2020, 06:59 PM
Last Post: forest44
  Unindent does not match any outer intendation level -error MaartenRo 3 2,272 Jun-28-2020, 11:46 AM
Last Post: MaartenRo
  HELP! unindent does not match any outer indentation level blackjesus24 2 1,941 Jan-29-2020, 08:00 AM
Last Post: blackjesus24
  Running function from parent module which has a loop in it. ta2909i 1 2,683 Nov-18-2019, 07:04 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020