Previous

While Loops

3 / 4
Next

A while loop can be nested inside another while loop to handle tasks that require iterating over two dimensions. For example, to print a grid of numbers, the outer loop can control rows, and the inner loop can control columns.

row = 1
while row <= 4:
    col = 1
    while col <= 3:
        print('.', end='')
        col = col + 1
    print()
    row = row + 1

This prints four rows of three dots each. By default, the print function prints a newline. The end='' prevents this. The print() in the outer loop adds a newline after each row.

Send

Nested while loops

Hint
Advanced

Input an integer n. Print a multiplication table for numbers 1 to n. Each row should represent the multiples of a number from 1 to n. For example, if n is 3, the output should be:

1 2 3
2 4 6
3 6 9

Each number should be separated by a space, and each row should end with a newline.

Help
Solve
Reset
Run
Submit
 

Test results

Test result #
Expected output
Your output

Your email has been verified!