I'm trying to write a python program that lets the user input the month by number for example the month of March as 3 and outputs the number of days and that mount
Asked
Active
Viewed 141 times
1 Answers
2
Is this a school project?
def get_days(month):
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
return days[(month-1) % 12]
I didn't consider a leap year since it wasn't mentioned,
but if you need it:
def get_days(month, leap=False):
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
return days[month-1] if not leap or month != 2 else 29

Joonyoung Park
- 474
- 3
- 6
-
-
-
-
what's the point of using modulo in the first example? Can you just do days[month-1]? – Hofbr Sep 25 '20 at 02:37
-
this will work if you know its a leap year, but it's not just year%4, just FYI – Derek Eden Sep 25 '20 at 02:43
-
@B.Hoffman You can. It looks like ,for some weird reason, I assumed 13+ could be an input. – Joonyoung Park Sep 25 '20 at 03:42