vendredi 23 octobre 2020

Python Programming Exercise Help - Days in a month including leap year [duplicate]

I am learning conditional statements in Python. The following program is meant to display the number of days in a given month, accounting for leap years. I am using the repl.it online compiler, and each time I input any month or year the program prints "31" (is it a compiler issue?). Could someone explain why the program is printing "31" for every month? Also, do I need an else statement at the end or can the program terminate after the last elif statement? I've attached a jpeg of original question for your reference.

Thanks in advance for your help! I've tried to solve this problem many times, unsuccessfully. I could really use some insight.

'''

month = input("Enter month (1-12): ")

year = input("Enter year (xxxx): ")

if month == 2:

      if year % 100 == 0 and year % 400 == 0:

        print (29)

      if year % 100 != 0 and year % 4 == 0:

        print (29)

      else:

        print (28)

elif month == 1 or 3 or 5 or 7 or 8 or 10 or 12:

      print(31) 

elif month == 4 or 6 or 9 or 11:

        print(30)

'''

Aucun commentaire:

Enregistrer un commentaire