From any date entered, how can I add another number of days entered so that it gives me the new date?
D=int(input("Ingresar dia de la fecha:"))
while D<0 or D>31:
print("Dia no válido")
D=int(input("Ingresar dia de la fecha:"))
M=int(input("Ingresar mes correspondiente:"))
while M<0 or M>12:
print("Mes no válido")
M=int(input("Ingresar mes:"))
A=int(input("Ingresar año correspondiente:"))
while A<0:
print("Año no valido")
A=int(input("Ingresar año:"))
FI=int(input("Ingrese fecha a saber:"))
while FI<0:
print("No es válido")
FI=int(input("Ingresar fecha a saber:"))
if FI>=365:
print(D,M,(A+1))
If I do it like this with IF it would take me forever to declare all the possibilities, how should I do it in a simple way?
Using the
datetime
python module is trivial, because it includes everything needed to convert strings representing dates to their internal date representation and operators to add/subtract etc these dates and print them back.For example:
Execution example:
Now, I understand that this solution is not valid in your case because it is an introductory programming exercise, in which you must code what is
datetime
already done.In this case, a simple solution could be to convert the date that the user has given you into an integer that represents the number of days that have elapsed since the beginning of the year. To do this you can have a list with 12 elements that are the days of each month, add all the elements up to the month that the user has given you (not included) and add the day of the month to it.
To this integer you add the
delta
one that the user has given you and you will have another integer that represents the number of days elapsed since the beginning of the year of the first date, until the date sought. You have to convert that integer to year, month and day, for which you can divide by 365 to find the number of years elapsed, and keep the rest to see the number of days in the target year. Then you would again use the list of days per month. You subtract the days of each month from that list from that integer, while the result is positive, in order to find what month it is in. The day of the month will be the remaining amount.Things get slightly more complicated if you have to take leap years into account. It would be necessary to know the specific specifications of the problem to see if it would be necessary to deal with those cases.