{ifs anidado}

Lenguaje de Programación - Ingeniería Civil

Usando if/else anidados

llueve = True
temperatura = int(input('Ingresa temperatura: '))
if temperatura < 18:
    if llueve == True:
        print('Llevaré paraguas y abrigo')
    else:
        print('Solo llevaré abrigo')
else:
    print('No necesito paraguas ni abrigo')
# PRESENTING CODE

¿Cuál es la salida de este programa?

x = 5
if x < 15:
  if x > 8:
    print('one')
  else:
	print('two')
else:
  if x > 2:
    print('three')
# PRESENTING CODE

A. one

B. two

C. three

D. más de una de las anteriores

E. no presenta nada

¿Qué imprime esto? (¡fíjese en los cambios!)

x = 5
if x < 15:
  if x > 8:
    print('one')
  else:
	print('two')

if x > 2:
  print('three')
# PRESENTING CODE

A. one

B. two

C. three

D. más de una de las anteriores

E. no presenta nada

¿Qué imprime esto? (¡note los nuevos cambios!)

x = 5
if x < 15:
  if x > 8:
    print('one')
else:
  print('two')

if x > 2:
  print('three')
# PRESENTING CODE

A. one

B. two

C. three

D. más de una de las anteriores

E. no presenta nada

avg = 95

if avg >= 90:
  grade = 'A'
if avg >= 80:
  grade = 'B'
if avg >= 70:
  grade = 'C'
if avg >= 60:
  grade = 'D'
else:
  grade = 'F'
  
print(grade)
# PRESENTING CODE

Decisiones multidireccionales

>> 'D'

# Aquí está la versión corregida:

avg = 95

if avg >= 90:
  grade = 'A'
elif avg >= 80:
  grade = 'B'
elif avg >= 70:
  grade = 'C'
elif avg >= 60:
  grade = 'D'
else:
  grade = 'F'
  
print(grade)
# PRESENTING CODE

Decisiones multidireccionales

>> 'A'

Decisiones multidireccionales: Sentencia if-elif-else

if condition1:
  # true block for condition1
elif condition2:
  # true block for condition2
elif condition3:
  # true block for condition3
...
else:
  # false block
# PRESENTING CODE

Las condiciones se evalúan en orden. Se ejecuta el bloque verdadero de la primera condición verdadera.

Si ninguna de las condiciones es verdadera, se ejecuta el bloque falso.

Sintaxis

¿Cuántas líneas imprime?

x = 5
if x == 8:
  print('how')
elif x > 1:
  print('now')
elif x < 20:
  print('brown')
print('cow')
# PRESENTING CODE

A. 0

B. 1

C. 2

D. 3

E. 4

# The following also involves decisions based on a person's age:

age = ... # let the user enter his/her age

if age < 13:
  print('You are a child.')
elif age >= 13 and age < 20:
  print('You are a teenager.')
elif age >= 20 and age < 30:
  print('You are in your twenties')
elif age >= 30 and age < 40:
  print('You are in your thirties')
else:
  print('You are a survivor')
# PRESENTING CODE

Evite el código demasiado complicado

¿Cómo se podría simplificar?

# The following also involves decisions based on a person's age:

age = ... # let the user enter his/her age

if age < 13:
  print('You are a child.')
elif age < 20:
  print('You are a teenager.')
elif age < 30:
  print('You are in your twenties')
elif age < 40:
  print('You are in your thisties')
else:
  print('You are a survivor')
# PRESENTING CODE

Evite el código demasiado complicado

Ptyhon: Condicionales

By lsantiago

Ptyhon: Condicionales

  • 44