samedi 29 mai 2021

Next calendar day invalid range [duplicate]

I am a beginner and this is an exercise I am expanding on. Basically given 3 input day, month, year it needs to return the following day. It works almost perfectly but I can't manage to print the correct date (ex: 31/5/2020 to 1/6/2020) because it print "invalid range" given I set 31 for days, given that this months (4,6,9,11) have only 30 days. I else want to print "invalid range" in the case I give 31 to a month that have only 30 days. This is my code so far:

`def next_day():

day = int(input("Input a day: "))
month = int(input("Input a month: "))
year = int(input("Input a year: "))
print(f'Current date is : {day}/{month}/{year}')

# for months with 30 days
if month == (4 or 6 or 9 or 11) and day == 30: 
    month += 1
    day = 1
# for months with 31 days ###ISSUE HERE
elif month == (1 or 3 or 5 or 7 or 8 or 10) and day == 31: 
    month += 1
    day = 1
else:
    day += 1

# for out of range values
if  day > 31 or month > 12:
    print("Invalid range")
    return False

# for leap years
if month == 2 and year % 4 == 0 and day == 28:
    day += 1
elif year % 4 != 0:
    month += 1
    day = 1

# for new year 
if month == 12 and day == 31:
    day = 1
    month = 1
    year += 1

print(f'Next date is : {day}/{month}/{year}')

Else is there a way to reformat the if statement in a simpler way without using so many or? Any suggestions?

Aucun commentaire:

Enregistrer un commentaire