Playground - Averigua si un año es bisiesto
Clase 8 de 56 • 21 Días de Python
Contenido del curso
Clase 8 de 56 • 21 Días de Python
Contenido del curso
Oscar Barajas Tavares
Manuel Felipe Acuña
Manuel Felipe Acuña
Sara María Mejia Sánchez
Brayan Barroso
Jonatan Martin
Brontis Xavier Anaya Torres
Facundo Rios
Juan Camilo Mesa Muñoz
Luis Rodrigo Magallan Jiménez
Nicolas Alpargatero
jean.joven
David Esteban Giraldo Duque
José Daniel Navarrete Díaz
Santiago Fernando
Gustavo Larco
Anette Iniesta Garcia
Luis Perez
Kelly Valderrama Gonzalez
Diego Mauricio Zuluaga Rodríguez
JORGE MANRIQUE CHAVARRO
Jeyson David Vargas Crespo
Owen Enríquez
Jose Eleazar Gavidia Diaz
Nicolas Alpargatero
Esteban Baldoni
Michael Chau
Juan Camilo Mesa Muñoz
profe, tengo una pregunta yo hice el código de esta manera
< def is_leap_year(year): # tu código aquí 👈 if year % 4 == 0 and year % 100 != 0 or year % 100 == 0 and year % 400 == 0: return ("True") else: return ("False") result= is_leap_year(2000) print (result) > tu me podrias decir si en el if esas multiples condiciones que puse estan bien ?
me podrias explicar si el if que hice en el codigo de abaja esta bien ?
......
Mi solucion
def is_leap_year(year): # tu código aquí 👈 if year > 0: if(year % 4 == 0) and (year % 100 != 0): return True elif (year % 100 == 0) and (year % 400 == 0): return True else: return False else: return False result = is_leap_year(-2004) print(result)
Cierto que se pueden hacer un if dentro de otro if, es decir if aninado. Muchas gracias men
Soy nuevo en Python y batalle bastante pero al fin pude, o eso creo jejejeje!
Muy bien rey
Mi código:
def is_leap_year(year): # tu código aquí 👈 year_is = False if year < 0: return year_is elif year % 4 == 0 and year % 100 != 0: return not year_is elif year % 100 == 0 and year % 400 == 0: return not year_is else: return year_is ```def is\_leap\_year(year): # tu código aquí 👈 year\_is = False if year < 0: return year\_is elif year % 4 == 0 and year % 100 != 0: return not year\_is elif year % 100 == 0 and year % 400 == 0: return not year\_is else: return year\_is
Una vez lo hice lo refactorice, debo confesar, estaba bueno el reto, me costó bastante. Porque la guía confunde la parte de "Si es divisible por 100 debe serlo por 400 también.", confunde porque el anterior enunciado decía, pero no por 100. .
En wiki fue más claro:
. Un año es bisiesto si es:
para establecer si un año dado es bisiesto: es bisiesto si es divisible entre cuatro y (no es divisible entre 100 o es divisible entre 400).
def is_leap_year(year): if year%4==0 and year%100 !=0 and year >0: return True elif year%100==0 and year%400==0 and year >0: return True else:return False
Mi solución: . . . . . . . . . .
def is_leap_year(year): if year % 4 == 0 and year > 0: if year % 100 != 0: return True elif year % 400 == 0: return True else: return False else: return False
Aqui una solución mas compacta
Soy ingeniero de software, codifico en C# en una empresa aca en Estados Unidos, y como estamos evolucionando a empresa de IA, decidi aprender python, y aun teniendo 1 year de experiencia, no me salio este ejercicio.
Mi solución:
def is_leap_year(year): if (year <=0): return False elif (year % 4 == 0) and (year % 100 !=0): return True elif (year % 100 == 0) and (year % 400 == 0): return True else: return False result = is_leap_year(2000) print(result) result = is_leap_year(-2024) print(result) result = is_leap_year(1984.25) print(result)
def is_leap_year(year): return (year % 4 == 0 and year % 100 != 0 or year % 100 == 0 and year % 400 == 0) and year > 0
para evitar el uso de booleanos literales como True y False y mas de un return (me han enseñado dicho que no es la mejor practitica.. me rompi el coco para hacerlo como Booleano en una forma logica sin if ni nada) en la ultima parte es year mayor que sero.. pero me lo cambia
Mi solución
def is_leap_year(year): if type(year) == float: return False elif year <= 0: return False elif (year%4)==0 and (year%100)!=0: return True elif (year % 4)==0 and (year % 100)==0 and (year % 400)==0: return True else: return False
Comparto el código
def is_leap_year(year): # Verificar si es un número (int o float) if not isinstance(year, (int, float)): return False # Verificar si es un número entero (aunque esté en formato float) if year != int(year): return False # Convertir a entero y verificar si es negativo year = int(year) if year < 0: return False # Aplicar reglas de años bisiestos if year % 4 == 0: if year % 100 == 0: return year % 400 == 0 return True return False pass # Solicitar entrada al usuario year_input = input("Ingrese un año: ") try: # Intentar convertir la entrada a número year = float(year_input) result = is_leap_year(year) except ValueError: result = False print(result)
Desarrollo del ejercicio
def is_leap_year(year): # tu código aquí 👈 divisible_4 = year % 4 == 0 divisible_100 = year % 100 == 0 divisible_400 = year % 400 == 0 if type(year) == int and year >= 0: # Comprueba que sea un número entero y además que sea positivo if (divisible_4 and not divisible_100) or (divisible_100 and divisible_400): return True else: return False else: return False
Hola, no entendí en la solución como hacer que sea capaz de manejar valores negativos, me pueden explicar porfa.
Gracias
Hola, Jeyson. Al no haber años negativos, cualquier cantidad que sea menor a cero será considerada como False (año no bisiesto), por lo que para cumplir esta condición, un if es suficiente.
if year <= 0: return False
Espero haber resuelto tu duda. Saludos ✌️
def is_leap_year(year): # tu código aquí 👈 if not isinstance(year, int) or year < 1: return False elif year % 4 == 0 and year % 100 !=0: return True elif year % 400 == 0: return True else: return False pass
Tu podrías darle a todas clases siguiente y tratar de pasar el examen final el del certificado, pero si aun así haces trampa en el examen pues solo te engañas tu.
No entiendo porque no pasa el test de negativos. Probando desde la consola el test de negativos funciona Mi funcion es def is_leap_year(year): return ((year % 4 == 0) and ( (year % 100 != 0) or (year % 400 == 0 )))
y devuelve correctos los años bisiestos, el test con decimales y no bisiestos, pero con los negativos devuelve
assert True == False
Probando desde la consola obtengo
year = -2024 print((year % 4 == 0) and ( (year % 100 != 0) or (year % 400 == 0 ))) True
Creo que una vez te indica un error con el test. se debe actualizar la web y volver a colocar el codigo correcto para realizar nuevamente el test y lo supere.
No pasa el test de negativos porque debes agregar (and year >= 0:) de lo contrarío, -2024 pasará las otras condiciones que pusiste.