Search code examples
python-3.xfor-loopwhile-loopcode-conversion

Not Able To use for-loop


** I want to use two for-loops instead of two while-loops **

i = 7
while i >= 1:
    j = i
    while j <= 7:
        print(j, end ="  ")
        J += 1
    i -= 1
    print()

Solution

  • The following is the for-loop equivalent:

    for i in range(7, 0, -1):
        j = i
        for j in range(i, 8):
            print(j, end="  ")
        print()
    

    The key is the correct use of range(start, stop, step). See also this.