Iteration While Nested Loop

Can someone help me figure out this nested while loop

This is what I was given to do:

And this is what I have so far:

hmmm okay i think i see the problem(s)?

so first trace the program ofc...

n is set as one

row is set as an integer from user input (so ima say “5”)

5 > 0 so:

1 <= 5:

print… uhhhh i dont think it’d print anything? so there’s the first problem.

first problem

there’s a comma there in line 6, that might be a problem- (and is end=“” supposed to be there? i doubt that’s necessary..)

okay, so i tested the code, the end="" is defo not supposed to be there…)

you don’t really need a var there (nor do u need a string?), so just put print(n) instead…

continue tracing (cuz the program still doesn't work)

n is set as one

row is set as an integer from user input (so ima say “5”)

5 > 0 so:

1 <= 5:

prints out 1

increment n by 1

…and there’s some unnecessary clutter here (don’t want that, that’d make tracing more difficult :>)

clutter

so we can just… delete some stuff before continuing :3

delete lines 8-9… annnnd when i test the code, it outputs this:

How many rows? 5
1
2
3
4
5

hm, ok, at least it isn’t stuck now? it prints out 5 rows, which is progress.. still gotta continue tracing

continue (again) tracing

5 is > 0 so:

1 <= 5:

prints out 1

increment n by 1

2 <= 5:

prints out…

hmm okay i see now what the next thing u gotta do… gotta do some concatenation..

so, you gotta make it so n = 1 and have it print out str(n) + str(n+1)...

code so far:

n=0

row = int(input("How many rows? "))

if row > 0:
    while n <= row:
        if n == 0:
            print(n+1)
        else:
            print(str(n) + str(n+1))
        n += 1
else:
    print("# of rows can't be negative")

…this outputs this:

How many rows? 5
1
12
23
34
45
56

sooo that’s even more progress :DD (sorry if this explanation is long, i’m still a beginner myself…)

okay, so i did the math,

when n=1:

(n) + (n+1) + (2n+1) + (3n+1) + (4n+1) is the formula for printing the 5th row…

(ack ew math get it outta here–)

so how do u do this formula in code? boom:

n=0

row = int(input("How many rows? "))

if row > 0:
    while n+1 <= row:
        n += 1
        spitOut = ""
        smth = 0
        for nyeheheh in range(n):
            smth += 1
            spitOut += str(1 + nyeheheh)
        print(spitOut)
else:
    print("# of rows can't be negative")

now it should work yay

now make it print a newline \n and tweak the formula code to invert back to output this:

12345

1234

123

12

1

and you’ll get the intended output :DD

(explanation is a bit rushed at the end cuz i gtg switch classes)

and that’s it :3 (first post completed yay)

Good morning, even if it is evening.

Here I’ve made a simple demo program,
that You could adapt further for Your needs.

So, You could have same functionality using any kind of loop: for, while, etc.

Happy :snake: coding!

try:
    while True:
        try:
            n = int(input("Enter the number of rows: "))
        except ValueError:
            print("Please enter a positive integer\n")
            continue

        if n < 0:
            print("Only positive integers please!\n")
            continue

        for i in range(1, n + 1):
            for j in range(1, i + 1):
                print(j, end=" ")
            print()

        print()

        for i in range(n, 0, -1):
            for j in range(1, i + 1):
                print(j, end=" ")
            print()
except KeyboardInterrupt:
    print("\nBye!")
/home/me/Pycharm/WelcomeScreen/.venv/bin/python Bb.py 
Enter the number of rows: 3.14
Please enter a positive integer

Enter the number of rows: -10
Only positive integers please!

Enter the number of rows: 1
1 

1 
Enter the number of rows: 9
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 

1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
Enter the number of rows: 
Bye!

Process finished with exit code 0