17. Nested for statements

A programming structure is said to be nested when it is inside another.

Each extra level with another for statement will have to be indented, that is, with spaces to the left.

This is an example with the for statement nested in two levels which generates a multiplication table:

print('Tabla de multiplicar:')
print('\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10')

for i in range(1, 11):
    print(i, end='\t')
    for j in range(1, 11):
       print(i*j, end='\t')
    print()

Result:

Tabla de multiplicar:
    1       2       3       4       5       6       7       8       9       10
1   1       2       3       4       5       6       7       8       9       10
2   2       4       6       8       10      12      14      16      18      20
3   3       6       9       12      15      18      21      24      27      30
4   4       8       12      16      20      24      28      32      36      40
5   5       10      15      20      25      30      35      40      45      50
6   6       12      18      24      30      36      42      48      54      60
7   7       14      21      28      35      42      49      56      63      70
8   8       16      24      32      40      48      56      64      72      80
9   9       18      27      36      45      54      63      72      81      90
10  10      20      30      40      50      60      70      80      90      100

The character '\t' means tab and is used to separate two numbers into 8 characters.

Exercises

  1. Write a program that asks for a number and answers if that number is the result of multiplying two numbers from 1 to 10.

    Clue:

    num = input('Escribe un número: ')
    num = int(num)
    
    for i in range(1, 11):
        for j in range(1, 11):
            if ... :
                print('El número', num, 'es igual a', i, 'x', j)
    
  2. Write a program that generates a triangle of asterisks thanks to a nested loop. On the first line you need to draw an asterisk. On the second line you need to draw two asterisks. It will continue like this until ten asterisks are drawn on the tenth line

    Clue:

    for i in range(...):
        for j in range(...):
            print('*', end='')
        print()  # Nueva línea
    

    Exit:

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    
  3. Modify the above program so that it prints a triangle of numbers as shown below:

    1
    22
    333
    4444
    55555
    666666
    7777777
    88888888
    999999999
    
  4. Draw an inverted triangle starting with 10 asterisks and ending with a single asterisk.

    Exit:

    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    
  5. Write a program that draws an asterisk chess with a nested loop that prints an asterisk when the sum of the two coordinates is divisible by 2. Otherwise it prints a space.

    El tamaño de la cuadrícula será de 10 filas por 10 columnas.

    Remember that at the end of each line a new line must be printed with the print() instruction.

    Clue:

    for i in range(...):
        for j in range(...):
            if (i + j) % 2 == 0:
                ...
            else:
                ...
        ...
    

    Exit:

    * * * * *
     * * * * *
    * * * * *
     * * * * *
    * * * * *
     * * * * *
    * * * * *
     * * * * *
    * * * * *
     * * * * *
    
  6. Modify the program above so that it draws another dot pattern. Each point will appear when the sum of its coordinates is divisible by 3.

    Exit:

     *  *  *
    *  *  *  *
      *  *  *
     *  *  *
    *  *  *  *
      *  *  *
     *  *  *
    *  *  *  *
      *  *  *
     *  *  *