I would like help with the following code, I had to solve an exercise in which it takes a certain year as input data, and the program had to answer me with True or False if the year was a leap year or not, respectively, and I wrote the following:
def isYearLeap(year):
if year % 4 != 0:
return False
elif year % 100 != 0:
return True
elif year % 400 != 0:
return False
else:
return True
yr = int(input("Ingresa un año para saber si es bisiesto: "))
while True:
if yr != 0:
print(isYearLeap(yr))
yr = int(input("Ingresa un año para saber si es bisiesto: "))
else:
break
Now I must write another code in which I enter the year, and the month, and the program should answer me how many days the month of the entered year contains, and with the help of the course I could only get here:
def isYearLeap(year):
if year % 4 != 0:
return False
elif year % 100 != 0:
return True
elif year % 400 != 0:
return False
else:
return True
def daysInMonth(year, month):
monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if isYearLeap(year) and month == 2:
return 29
return monthDays[month - 1]
...but I don't know how to continue. The program should take 2 input data (the year and the month) and as output data return the number of days but I don't know how to do it and I would really appreciate it if you help me a little. From already thank you very much. Cheers
EDITION
When I try to add the following if-break segment (so that if I enter 0 in the month AND(and) 0 in the year the code stops asking for the data and ends), the code ends no matter what data I enter .
while True:
mo = int(input("Ingrese el mes: "))
yr = int(input("Ingrese al año: "))
if mo and yr == 0:
break
else:
print ("El mes", mo, "del", yr, "contiene", daysInMonth(mo, yr), "dias.")
You are not missing anything, just ask for the month and year and then get the number of days.
This minimal code shows how to ask for month and year by console, and how to print the result:
With the function
input()
you enter a text from the keyboard, which we convert to an integer usingint()
. Validations are pending.We then use
print()
with f-string to produce the output.An f-string is a string containing Python expressions enclosed in
{}
. You have to prepend the letter "f" to the string to get those expressions to be calculated and replaced in the text. Without the "f", the string is printed as is.show
produces:
You can put a variable in the list, so that function can be rewritten like this: