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.

Este es un ejemplo con la instrucción for anidada en dos niveles que genera una tabla de multiplicar:

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. Escribe un programa que pida un número y te responda si ese número es el resultado de multiplicar dos números en el rango del 1 al 10.

    Clue:

    num = int( input('Escribe un número: ') )
    
    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. Modifica el programa anterior para que dibuje otro patrón de puntos. Cada punto aparecerá cuando la suma de sus coordenadas sea divisible por 3.

    Exit:

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