While Loops

3 / 5

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. Consider this example to print a 4x3 grid of dots:

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

This prints two rows of three dots each. The end='' in print prevents a newline, and the outer loop’s print() adds a newline after each row.

Nested while loops

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.

Congratulations!

You passed the !

Welcome!

Would you like a guided tour on how to use Vibewise?

Your email has been verified!