Ladder. Given an integer n (where n ≤ 9), print a ladder of n steps. The k-th step consists of the integers from 1 to k without spaces between them.
|
n = int(input()) for i in range(1, n + 1): for j in range(1, i + 1): print(j, end='') print() |
- Reading Input:
- The
input()
function reads a line of text from the user.
- The
int()
function converts this input to an integer and stores it in the variable n
.
- Printing the Ladder:
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end=”)
print()
- The outer
for
loop iterates from 1 to nnn (inclusive), representing each step of the ladder.
- The inner
for
loop iterates from 1 to iii (inclusive), printing the numbers for the current step.
- The
print(j, end='')
statement prints each number j
on the same line without spaces.
- The
print()
statement at the end of the inner loop moves the cursor to the next line for the next step.