11. Nested if statements

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

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

This is an example with the if statement nested in two levels:

numero = int(input('Introduce un número: '))
if numero % 3 == 0:
    if numero % 5 == 0:
        print('El número', numero, 'es divisible por 3 y por 5')
    else:
        print('El número', numero, 'es divisible por 3')

Nesting if conditions can be useful, but try not to nest too many levels to make the code easier to read.

This is another example of if nested on two levels:

edad = int(input('Introduce tu edad: '))
if edad >= 18:
    if edad >= 65:
        print("Eres un adulto mayor")
    else:
        print("Eres un adulto")
else:
    if edad > 3:
        print("Eres menor de edad")
    else:
        print("Eres un bebé")

Exercises

  1. Write a program that calculates the price of a movie ticket depending on the age.

    The normal price of the entrance is 10 Euros.

    If the client is 60 or older, they will only pay 2 Euros.

    If the client is 12 years old or less, he will only pay 4 Euros.

    Clue:

    edad = int( input('Escribe tu edad: ') )
    if ...
    
  2. Write a program to check if a person can ride the rides in a park.

    If the height is less than 140, you can not go on the attractions

    Otherwise:

    • If the age is less than 12 years old, you can go on the attractions for children.
    • If the age is greater than or equal to 12 years, you can go up to all the attractions.

    Clue:

    altura = 120
    edad = 5
    if ....
    
  3. Write a program that asks for the grade obtained and that answers according to the following conditions:

    If the grade is greater than or equal to 5 -> You have passed

    If the grade is greater than 6 -> You have passed with a good grade

    If the note is less than 5 -> You have failed

    If the grade is less than 4 -> You have failed with a bad grade

  4. Write a helper program to repair objects.

    The program must first ask if the object is moving. Then you must ask if the object should move. The program should then print a message on the screen depending on the options entered:

    If the object moves and it should move -> No need to repair.

    If the object is moving and it shouldn't be moving -> Use duct tape.

    If the object does not move and it should not move -> No need to repair.

    If the object does not move and it should move -> Use 3 in 1 oil.

    Clue:

    # Pregunta para obtener una respuesta de tipo si/no
    # con una sola letra (s/n)
    se_mueve = input('El objeto se mueve? (s/n)')
    if se_mueve == 's':
        ...
    
  5. Write a program to identify characters from the Asterix and Obelix comics.

    If the character is the main protagonist:

    • If the character raises menhirs: it is Obelix.
    • Otherwise: it's Asterix.

    Otherwise:

    • If the character brews potions: it is Panoramix.

    • Otherwise:

      • If the character sings badly: it is Assurancetúrix.
      • Otherwise: the character is not identified.

    Clue:

    # Pregunta para obtener una respuesta de tipo si/no
    # con una sola letra (s/n)
    opcion_1 = input('El personaje es protagonista principal? (s/n)')
    if opcion_1 == 's':
    
  6. Write a program that asks for the username and then the password. If the user is equal to 'admin' then it checks the password, which should be equal to 'secret'. In case of writing the correct password, 'successful login' should be printed.

    If the password is wrong, write 'Wrong password'

    If the user is not 'admin', write 'Wrong User'

  7. Write a program to classify triangles.

    First, the lengths of the sides must be defined in the program:

    lado1 = 5
    lado2 = 3
    lado3 = 4
    

    Next, the lengths of the sides must be checked and the type of triangle printed on the screen:

    • If side1 == side2 and if side2 == side3: is equilateral
    • If side1 == side2 or side1 == side3 or side2 == side3: is isosceles
    • Otherwise: it is scalene.
  8. Write a program that checks if a year is a leap year. A year is a leap year if it is divisible by 4 (year % 4 == 0).

    Of these leap years, we must eliminate those that are divisible by 100 and add those that are divisible by 400.

    Clue:

    anio = int(input('Escribe el año: '))
    if anio % 4 == 0:
        if anio % 100 == 0:
            if anio % 400 == 0:
                print('...')
            else:
                print('...')
        else:
            print('...')
    else:
        print('...')
    

    The list of leap years between 1600 and 2400 is:

    1600, 1604, 1608, 1612, 1616,
    1620, 1624, 1628, 1632, 1636,
    1640, 1644, 1648, 1652, 1656,
    1660, 1664, 1668, 1672, 1676,
    1680, 1684, 1688, 1692, 1696,
    1704, 1708, 1712, 1716, 1720,
    1724, 1728, 1732, 1736, 1740,
    1744, 1748, 1752, 1756, 1760,
    1764, 1768, 1772, 1776, 1780,
    1784, 1788, 1792, 1796, 1804,
    1808, 1812, 1816, 1820, 1824,
    1828, 1832, 1836, 1840, 1844,
    1848, 1852, 1856, 1860, 1864,
    1868, 1872, 1876, 1880, 1884,
    1888, 1892, 1896, 1904, 1908,
    1912, 1916, 1920, 1924, 1928,
    1932, 1936, 1940, 1944, 1948,
    1952, 1956, 1960, 1964, 1968,
    1972, 1976, 1980, 1984, 1988,
    1992, 1996, 2000, 2004, 2008,
    2012, 2016, 2020, 2024, 2028,
    2032, 2036, 2040, 2044, 2048,
    2052, 2056, 2060, 2064, 2068,
    2072, 2076, 2080, 2084, 2088,
    2092, 2096, 2104, 2108, 2112,
    2116, 2120, 2124, 2128, 2132,
    2136, 2140, 2144, 2148, 2152,
    2156, 2160, 2164, 2168, 2172,
    2176, 2180, 2184, 2188, 2192,
    2196, 2204, 2208, 2212, 2216,
    2220, 2224, 2228, 2232, 2236,
    2240, 2244, 2248, 2252, 2256,
    2260, 2264, 2268, 2272, 2276,
    2280, 2284, 2288, 2292, 2296,
    2304, 2308, 2312, 2316, 2320,
    2324, 2328, 2332, 2336, 2340,
    2344, 2348, 2352, 2356, 2360,
    2364, 2368, 2372, 2376, 2380,
    2384, 2388, 2392, 2396, 2400
    

    The years 1700, 1800, 1900, 2100, 2200, 2300 should not appear in the list because they are divisible by 100.

    The years 1600, 2000 and 2400 should appear in the list because, although they are divisible by 100, they are also divisible by 400.